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

StackOverflow https://stackoverflow.com/questions/23273230

Pregunta

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

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 cannot be 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)

Please find below simplified code listing :-

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

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

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:

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

¿Fue útil?

Solución

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().

Otros consejos

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.

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.

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:

mock-maker-inline

please refer article https://www.baeldung.com/mockito-final

If you are using @Mock and getting this same error, it might be due to mocks not functioning properly. For me, adding MockitoAnnotations.initMocks(this); in setup() resolved the error.

Simple, Should use @Spy annotation with @InjectMocks annotation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top