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

2023-05-02Java开发问题
1706

本文介绍了此处检测到错位的参数匹配器.您不能在 Mockito 中的验证或存根之外使用参数匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn vpn下载

问题描述

免费vpn vpn free free vpn 限时送ChatGPT账号.. vpn下载 免费vpn 免费vpn vpn free 免费vpn

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

Out of the following two test cases 免费vpn下载 in BundleProcessorTest.java, i am getting below exception, although, my first test case vpn free passes successfully.

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

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

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

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

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

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"))

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

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

在bundle.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull(BundleProcessorTest.java:28)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)

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

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

Please find free vpn below simplified code listing :-

BundlePlugin.java

package bundle;

import java.util.List;

public class BundlePlugin {

    private final String pluginName ;
    private final vpn下载 List<String> featureContent ;

    public vpn free vpn下载 BundlePlugin(String pluginName, 免费vpn List<String> featureContent) {
        super();
   vpn free  vpn free   vpn free   this.pluginName = pluginName;
        this.featureContent 免费vpn下载 = featureContent;
    }

    public String vpn下载 getPluginName() {
        return pluginName;
    免费vpn }

   vpn下载  public List<String> getFeatureContent() {
  vpn下载       vpn下载 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) {

    vpn free     List<String> featureContent = 免费vpn 免费vpn下载 new ArrayList<String>() ;

  vpn free       return new BundlePlugin(pluginName, featureContent);
    }
}

BundleProcessorTest.java

package bundle.test;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyString;
import vpn free 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() free vpn {

        Iterator<String> artifactIterator = mock(Iterator.class) ;
    vpn下载     bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;
        assertNotNull( bundlePlugin );
    }

    @Test
    public void bundlePluginContentShouldNotBeNull() {
        Iterator<String> artifactIterator = mock(Iterator.class) ;
      vpn free   bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) vpn free ;

        List<String> featureContent = free vpn bundlePlugin.getFeatureContent() ;
        assertNotNull( featureContent );
 免费vpn下载    }
}

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

How to vpn下载 execute this test without problem.

编辑 1:

但如果我用 @Ignore vpn下载 注释标记 bundlePluginCollectionShouldNotBeNull 测试,那么第一个测试用例会毫无例外地通过.

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

推荐答案

你在调用测试方法时使用mockito anyString(),它应该只用于验证mock对象以确保使用测试中的任何字符串参数调用某个方法,但不调用测试本身.对于您的测试,请使用空字符串 "" 而不是 anyString().

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 免费vpn下载 called with 免费vpn any string parameter inside 免费vpn下载 the test, but not to invoke 免费vpn下载 the test itself. For your free vpn 免费vpn test use empty string "" instead free vpn to anyString().

这篇关于此处检测到错位的参数匹配器.您不能在 Mockito 中的验证或存根之外使用参数匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT vpn free using JAVA?(如何使用 JAVA 向 免费vpn COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page 免费vpn direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 free vpn Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use vpn下载 cyrillic .properties file in eclipse project(在 免费vpn Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way free vpn to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL vpn free 语言?)...
2024-08-25 Java开发问题
vpn free 11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I 免费vpn下载 change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13