An invalid argument argument was found. You cannot use argument arguments outside of validation or stubbing in Mockito

From the following two test cases in BundleProcessorTest.java, I get below exceptions, although my first test case succeeds.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: An invalid argument argument was detected:

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

You cannot use argument arguments outside of validation or stubbing. Examples of the correct use of arguments: when (mock.get (anyInt ())) thenReturn (zero). doThrow (new RuntimeException ()). when (mock) .someVoidMethod (anyObject ()); check (layout) .someMethod (contains ("Foo"))

In addition, this error may occur because you use matching arguments with methods that cannot be mocked. The following methods cannot be stubbed / verified: final / private / equals () / hashCode ().

in bundle.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull (BundleProcessorTest.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) in sun.reflect.NativeMethodAccessorImpl.invoke (unknown)

The following is a simplified list of codes: -

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 run this test without problems.




Change 1:

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

+25
java unit-testing junit mockito
Apr 24 '14 at 15:25
source share
2 answers

You use mockito anyString() when invoking the test method, it should only be used to test the mock object to ensure that any method is called with any string parameter inside the test, but not to call the test itself. For your test, use the empty string "" instead of anyString() .

+40
Apr 24 '14 at 15:41
source share

Ideally, anyString () should not be used outside the mock or verify block. I ran into the same problem. Changing the value of anyString () for some string ("xyz") works fine.

Note: Please note that you can use anyString () for some other methods, which causes some other methods to crash. It took my hour to figure it out. My real test method received passes individually, but when I tried to run it in a hole, it got a failure due to the reason that some other test case used anyString () outside to check or check the block.

0
Jul 26 '19 at 11:20
source share



All Articles