错误的参数匹配器在这里检测到.您不能在Mockito中使用验证或存根之外的参数匹配器

mog*_*gli 25 java junit unit-testing mockito

BundleProcessorTest.java中的以下两个测试用例中,我得到的是异常,但是,我的第一个测试用例成功通过了.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:此处检测到错位的参数匹配器:

- > at bundle.test.BundleProcessorTest.bundlePluginShouldNotBeNull(BundleProcessorTest.java:22)

您不能在验证或存根之外使用参数匹配器.正确使用参数匹配器的示例:when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); 验证(模拟).someMethod(包含( "富"))

此外,此错误可能会显示,因为您使用参数匹配器与无法模拟的方法.以下方法无法进行存根/验证:final/private/equals()/ hashCode().

at the package.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull(BundleProcessorTest.java:28)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

请在下面找到简化的代码清单: -

BundlePlugin.java

package bundle;

import java.util.List;

public class BundlePlugin {

    private final String pluginName ;
    private final List<String> featureContent ;

    public BundlePlugin(String pluginName, List<String> featureContent) {
        super();
        this.pluginName = pluginName;
        this.featureContent = featureContent;
    }

    public String getPluginName() {
        return pluginName;
    }

    public List<String> getFeatureContent() {
        return featureContent;
    }
}
Run Code Online (Sandbox Code Playgroud)

BundleProcessor.java

package bundle;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BundleProcessor {

    public BundlePlugin getBundlePlugin(String pluginName, Iterator<String> artifactIterator) {

        List<String> featureContent = new ArrayList<String>() ;

        return new BundlePlugin(pluginName, featureContent);
    }
}
Run Code Online (Sandbox Code Playgroud)

BundleProcessorTest.java

package bundle.test;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;

import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import bundle.BundleProcessor;

public class BundleProcessorTest {

    BundleProcessor bundleProcessor = new BundleProcessor() ;   

    @Test
    public void bundlePluginShouldNotBeNull() {

        Iterator<String> artifactIterator = mock(Iterator.class) ;
        bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;
        assertNotNull( bundlePlugin );
    }

    @Test
    public void bundlePluginContentShouldNotBeNull() {
        Iterator<String> artifactIterator = mock(Iterator.class) ;
        bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;

        List<String> featureContent = bundlePlugin.getFeatureContent() ;
        assertNotNull( featureContent );
    }
}
Run Code Online (Sandbox Code Playgroud)

如何毫无问题地执行此测试.


编辑1:

但是如果我用@Ignore注释标记bundlePluginCollectionShouldNotBeNull测试,那么第一个测试用例通过而没有任何异常.

小智 40

anyString()在调用测试方法时使用mockito ,它应该仅用于验证模拟对象,以确保在测试中使用任何字符串参数调用某个方法,但不调用测试本身.对于您的测试,请使用空字符串""代替anyString().

  • `MockitoJUnitRunner`添加了一个`@ After`方法,调用[`Mockito.validateMockitoUsage`](http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#validateMockitoUsage%28%29) .使用普通的运行程序,JUnit以无效状态结束测试,但无法检测到它.我在这里提供了更多详细信息[http://stackoverflow.com/q/22822512/1426891]. (4认同)
  • @rits Mockito没有验证"模式",因此如果两个测试都运行,mockito认为`anyString()`与`bundlePluginShouldNotBeNull`测试有关.使用MockitoJUnitRunner可能会捕获此问题和/或为您提供更好的错误消息. (2认同)