Java 此处检测到错位的参数匹配器。您不能在 Mockito 中的验证或存根之外使用参数匹配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23273230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:36:50  来源:igfitidea点击:

Misplaced argument matcher detected here. You cannot use argument matchers outside of verification or stubbing in Mockito

javaunit-testingjunitmockito

提问by mogli

Out of the following two test cases in BundleProcessorTest.java, i am getting below exception, although, my first test case passes successfully.

BundleProcessorTest.java中的以下两个测试用例中,我遇到了异常,尽管我的第一个测试用例成功通过。

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected here:

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

You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannotbe stubbed/verified: final/private/equals()/hashCode().

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

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

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

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

此外,此错误可能会出现,因为您将参数匹配器与无法模拟的方法一起使用。以下方法不能被存根/验证:final/private/equals()/hashCode()。

在 bundle.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull(BundleProcessorTest.java:28) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

Please find below simplified code listing :-

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

BundlePlugin.java

捆绑插件.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;
    }
}

BundleProcessor.java

捆绑处理器.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);
    }
}

BundleProcessorTest.java

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 );
    }
}

How to execute this test without problem.

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



Edit 1:

编辑1:

But if i mark the bundlePluginCollectionShouldNotBeNulltest with @Ignore annotation, then first test case passes without any exception.

但是如果我用@Ignore 注释标记bundlePluginCollectionShouldNotBeNull测试,那么第一个测试用例毫无例外地通过。

采纳答案by Zahid M

You are using mockito anyString()while calling the test method, it should be used only for verifying a mock object to ensure a certain method is called with any string parameter inside the test, but not to invoke the test itself. For your test use empty string ""instead to anyString().

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

回答by Ashutosh Sharma

Ideally anyString() should not be used outside the mock or verify block.I was facing the same issue.Changing the anyString() to some string ("xyz") value works fine.

理想情况下,不应在模拟或验证块之外使用 anyString()。我遇到了同样的问题。将 anyString() 更改为某个字符串 ("xyz") 值工作正常。

Note :Make a note that you might use anyString() to some other methods that leads in failure of some other method. It wasted my one hour to figure it out. My actual test method was getting passes individually but when i was trying to run that in a hole it was getting failed because of the reason that some other test case was using anyString() outside to mock or verify block.

注意:请注意,您可能会将 anyString() 用于其他一些方法,从而导致其他方法失败。浪费了我一个小时来弄清楚。我的实际测试方法是单独通过,但是当我试图在一个洞中运行它时它失败了,因为其他一些测试用例在外面使用 anyString() 来模拟或验证块。