1

我是 Groovy 的新手(同样也是 JMock 的新手),并且在构建对被模拟方法的参数使用匹配器的期望时遇到了一些麻烦。当我尝试做这样的事情时:

    Expectations e = new Expectations();
    e.allowing(mockObject).doSomething(Expectations.with(aNonNull(ImmutableCollection.class)))
    e.will(returnValue(someResponse))

在构建期望时会导致以下错误:

groovy.lang.MissingMethodException: No signature of method: static org.jmock.Expectations.with() is applicable for argument types: (org.hamcrest.core.IsNot) values: [not null]
Possible solutions: with(boolean), with(org.hamcrest.Matcher), with(byte), with(org.hamcrest.Matcher), with(char), with(org.hamcrest.Matcher)

aNonNull 返回Matcher<T>(org.hamcrest.core.IsNot implements Matcher<T>) 并且有一个 Expectations.with 方法接受一个 Matcher 所以我不知道为什么 Groovy 试图找到一个带有具体类而不是指定接口的版本由非空。我还尝试将 aNonNull 的返回值转换为 Matcher 并且Matcher<T>不对错误进行任何更改。我不确定泛型是否有一些东西让 Groovy 感到困惑,或者还有什么要检查的。

4

1 回答 1

1

根据 JavaDoc,org.jmock.Expectations.with() 是一个实例而不是静态方法。这就是你得到错误的原因。

顺便说一句,专门为 Groovy 构建的测试/模拟框架将使您的生活更轻松(即使在测试 Java 代码时)。例如,同样的期望在 Spock (http://spockframework.org) 中看起来像这样:

mockObject.doSomething(_ as ImmutableCollection) >> someResponse

另一个需要注意的 Groovy 模拟框架是 GMock (http://code.google.com/p/gmock/)。

于 2010-12-01T23:58:25.707 回答