【问题标题】:InvalidUseOfMatchersException when setting expectations in Mockito and Groovy 2.0.4在 Mockito 和 Groovy 2.0.4 中设置期望时出现 InvalidUseOfMatchersException
【发布时间】:2012-10-18 18:43:36
【问题描述】:

尝试使用模拟执行简单测试时遇到意外错误。

@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 是单一方法接口 Device::DeviceBuilder#build(File root)。根据 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 的 sn-p:

<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>

我的怀疑要么是 Groovy 管理类的方式有些奇怪,要么是版本不兼容,但我希望这是我看不到的显而易见的事情。

【问题讨论】:

  • 我看不出有什么问题。当您执行when(deviceBuilder.build(deviceRoot)).thenReturn(expectedDevice) 时会发生什么?
  • 我有同样的问题,我不能在 Groovy 上做基本的 hello world 存根
  • 对自己的回答晚了,我发现的问题是因为 deviceRoot 的元类。我通过将类切换到 Java 而不是 Groovy 解决了这个问题。

标签: exception groovy mockito


【解决方案1】:

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

问题中描述的问题在http://code.google.com/p/mockito/issues/detail?id=303有详细说明

我遇到了同样的问题,我使用https://github.com/cyrusinnovation/mockito-groovy-support提供的库解决了它

【讨论】:

  • 这不是我最终得到的答案,但我相信这也应该解决问题。谢谢
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我没有回答最初的问题。

    我到处搜索,关于我得到的错误的唯一问题指向这个讨论。

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

    【讨论】:

    • 我不完全确定我理解你的答案。方法 deviceBuilder.build 正在被测方法内部调用,因此 is 在验证之前。另一次在测试中使用它是因为这是设定的期望
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多