在Mockito和Groovy 2.0.4中设置期望值时InvalidUseofMatcherException无效

在Mockito和Groovy 2.0.4中设置期望值时InvalidUseofMatcherException无效,exception,groovy,mockito,Exception,Groovy,Mockito,在尝试使用模拟执行简单测试时,我遇到了一个意外错误 @RunWith(MockitoJUnitRunner) class AccessorTest { @Mock private DeviceBuilder deviceBuilder @Test void shouldCreateDeviceFromFilesystem() { //given URI uri = this.class.classLoader.getResour

在尝试使用模拟执行简单测试时,我遇到了一个意外错误

@RunWith(MockitoJUnitRunner)
class AccessorTest {

    @Mock
    private DeviceBuilder deviceBuilder

    @Test
    void shouldCreateDeviceFromFilesystem() {
        //given
        URI uri = this.class.classLoader.getResource("sample-filesystem").toURI()
        File deviceRoot = new File(uri)

        Accessor accessor = new Accessor(deviceBuilder)
        Device expectedDevice = new Device(deviceRoot)
        when(deviceBuilder.build(eq(deviceRoot))).thenReturn(expectedDevice)

        //when
        Device device = accessor.readFrom(deviceRoot)
        //then
        assert device == expectedDevice
        verify(deviceBuilder).build(deviceRoot)
    }
}
DeviceBuilder是一个单方法接口设备::DeviceBuilder#build(文件根)。根据Josh Bloch,设备有一个定义良好的equals方法

异常在when()行抛出,并且作用域中的所有变量都不为null。完全例外的情况是:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
0 matchers expected, 1 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
值得一提的是,这里有一段我的POM:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <compilerId>groovy-eclipse-compiler</compilerId>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>2.7.0-01</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.0.4</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.5</version>
        <scope>test</scope>
    </dependency>
</dependencies>

maven编译器插件
2.3.2
groovy eclipse编译器
org.codehaus.groovy
groovy eclipse编译器
2.7.0-01
org.codehaus.groovy
groovy all
2.0.4
朱尼特
朱尼特
4.10
测试
org.mockito
莫基托所有
1.8.5
测试

我的怀疑要么是Groovy管理类的方式有些奇怪,要么是版本不兼容,但我希望这是我看不到的明显现象。

有时Mockito在发生不适当的使用时,但在下一次调用时,不会标记不适当的使用。在那之前你可以看看测试,也许你有问题


这条消息说,在调用中,您要么只能提供匹配器,要么只能提供真实对象,但不能提供它们的组合。通常可以通过使用eq(obj)而不是对象本身(或sameInstance(obj))来修复这一问题,以拥有所有匹配器。但是,在这种情况下,我在您发布的代码中找不到任何符合该描述的地方,这就是为什么我怀疑代码的早期位置存在问题。

我知道这是一个老问题,但这可能对其他人有所帮助

问题中描述的问题详见


我遇到了同样的问题,我通过使用提供的库修复了它。我没有回答原来的问题

我到处都找遍了,唯一一个关于错误的问题就是我在这次讨论中找到了要点


如果有人遇到这个错误:不要调用mock方法为匹配器提供值。调用
deviceBuilder.build
应该在
verify(

我看不出有什么问题。在执行
when(deviceBuilder.build(deviceRoot))时会发生什么。然后返回(expectedDevice)
?我也有同样的问题,我不能在GroovyLate上对自己的答案进行基本的hello world stubing,我发现这个问题是因为deviceRoot的元类。我通过将类切换到Java而不是Groovy来解决这个问题。这不是我最终得到的答案,但是的,我相信这也可以解决这个问题。谢谢,我不完全是su我理解你的答案。方法deviceBuilder.build正在被测试的方法内部调用,因此在验证之前。在测试中使用它的另一次是因为这是设置的预期