简体   繁体   English

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

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

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: org.mockito.exceptions.misusing.InvalidUseOfMatchersException:此处检测到错位的参数匹配器:

-> at bundle.test.BundleProcessorTest.bundlePluginShouldNotBeNull(BundleProcessorTest.java:22) -> 在 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);参数匹配器的正确使用示例:when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo"))验证(模拟)。someMethod(包含(“foo”))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.此外,此错误可能会出现,因为您将参数匹配器与无法模拟的方法一起使用。 Following methods cannot be stubbed/verified: final/private/equals()/hashCode().以下方法不能被存根/验证: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)在 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 bundlePluginCollectionShouldNotBeNull test with @Ignore annotation, then first test case passes without any exception.但是如果我用 @Ignore 注释标记bundlePluginCollectionShouldNotBeNull测试,那么第一个测试用例毫无例外地通过。

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.您在调用测试方法时使用了 mockito anyString() ,它应该仅用于验证模拟对象以确保在测试中使用任何字符串参数调用某个方法,而不是调用测试本身。 For your test use empty string "" instead to anyString() .对于您的测试,请使用空字符串""代替anyString()

Ideally anyString() should not be used outside the mock or verify block.理想情况下,不应在模拟或验证块之外使用anyString() 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.注意:请注意,您可能会将anyString()用于其他一些方法,从而导致其他方法失败。 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()来模拟或验证块。

简单,应该将@Spy注释与@InjectMocks注释一起使用。

We need to add a text file to the project's src/test/resources/mockito-extensions directory named org.mockito.plugins.MockMaker and add a single line of text:我们需要在项目的 src/test/resources/mockito-extensions 目录中添加一个文本文件,名为 org.mockito.plugins.MockMaker 并添加一行文本:

mock-maker-inline mock-maker-inline

please refer article https://www.baeldung.com/mockito-final请参考文章https://www.baeldung.com/mockito-final

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用resthighlevelcl中的indexAsync方法获取“空指针异常”和“您不能在验证或存根之外使用参数匹配器” - Getting "Null Pointer Exception" and "You cannot use argument matchers outside of verification or Stubbing" using indexAsync method in resthighlevelcl Mockito&#39;在这里检测到错误的参数&#39;在Java中 - Mockito 'Misplaced argument detected here' in Java 为什么在这里通过anyInt()检测到Mockito放错位置的参数匹配器? - Why am I getting Mockito misplaced argument matcher detected here with anyInt()? Mockito.when 给出 InvalidUseOfMatchersException:此处检测到错误放置或误用的参数匹配器 - Mockito.when giving InvalidUseOfMatchersException: Misplaced or misused argument matcher detected here Mockito 和 Powermockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException:检测到错误放置的参数匹配器 - Mockito and Powermockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected org.mockito.exceptions.misusing.InvalidUseOfMatchersException:错位的参数匹配器 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException:Misplaced argument matcher org.mockito.exceptions.misusing.InvalidUseOfMatchersException错误放置的参数匹配器 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException Misplaced argument matcher 带有 arguments 的 MockedStatic 表示“检测到错误放置或误用的参数匹配器” - MockedStatic with arguments says "Misplaced or misused argument matcher detected" Mockito varargs参数匹配器的无效使用 - Mockito varargs Invalid use of argument matchers mockito 测试错误无效使用参数匹配器 - mockito test error Invalid use of argument matchers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM