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

4xrmg8kj  于 2023-06-04  发布在  Java
关注(0)|答案(5)|浏览(169)

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()); verify(mock).someMethod(contains(“foo”))
此外,此错误可能会显示,因为您使用参数匹配器的方法无法被模拟。以下方法 * 不能 * 被存根/验证: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)
请在下面找到简化的代码清单:

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

如何执行此测试没有问题。

编辑1:

但是如果我用@Ignore annotation标记bundlePluginCollectionShouldNotBeNull测试,那么第一个测试用例会无异常通过。

ftf50wuq

ftf50wuq1#

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

fivyi3re

fivyi3re2#

理想情况下,anyString()不应在mock或verify块之外使用。我也遇到了同样的问题。将anyString()更改为某个字符串(“xyz”)值可以正常工作。

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

tf7tbtn2

tf7tbtn23#

我们需要将一个文本文件添加到项目的src/test/resources/mockito-extensions目录中,名为org.mockito.plugins.MockMaker,并添加一行文本:
mock-maker-内联
请参阅文章https://www.baeldung.com/mockito-final

3ks5zfa0

3ks5zfa04#

如果您正在使用@Mock并得到相同的错误,则可能是由于模拟无法正常工作。对我来说,在setup()中添加MockitoAnnotations.initMocks(this);解决了这个错误。

vkc1a9a2

vkc1a9a25#

简单,应该使用@Spy注解和@InjectMocks注解。

相关问题