<bdo id='kG9BH'></bdo><ul id='kG9BH'></ul>

<i id='kG9BH'><tr id='kG9BH'><dt id='kG9BH'><q id='kG9BH'><span id='kG9BH'><b id='kG9BH'><form id='kG9BH'><ins id='kG9BH'></ins><ul id='kG9BH'></ul><sub id='kG9BH'></sub></form><legend id='kG9BH'></legend><bdo id='kG9BH'><pre id='kG9BH'><center id='kG9BH'></center></pre></bdo></b><th id='kG9BH'></th></span></q></dt></tr></i><div id='kG9BH'><tfoot id='kG9BH'></tfoot><dl id='kG9BH'><fieldset id='kG9BH'></fieldset></dl></div>

<small id='kG9BH'></small><noframes id='kG9BH'>

  • <tfoot id='kG9BH'></tfoot>
        <legend id='kG9BH'><style id='kG9BH'><dir id='kG9BH'><q id='kG9BH'></q></dir></style></legend>

        此处检测到错位的参数匹配器.您不能在 Mockito 中

        时间:2023-09-25
            <tbody id='CEtl1'></tbody>

          <small id='CEtl1'></small><noframes id='CEtl1'>

          <legend id='CEtl1'><style id='CEtl1'><dir id='CEtl1'><q id='CEtl1'></q></dir></style></legend>
        1. <tfoot id='CEtl1'></tfoot>
          <i id='CEtl1'><tr id='CEtl1'><dt id='CEtl1'><q id='CEtl1'><span id='CEtl1'><b id='CEtl1'><form id='CEtl1'><ins id='CEtl1'></ins><ul id='CEtl1'></ul><sub id='CEtl1'></sub></form><legend id='CEtl1'></legend><bdo id='CEtl1'><pre id='CEtl1'><center id='CEtl1'></center></pre></bdo></b><th id='CEtl1'></th></span></q></dt></tr></i><div id='CEtl1'><tfoot id='CEtl1'></tfoot><dl id='CEtl1'><fieldset id='CEtl1'></fieldset></dl></div>

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

                  问题描述

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

                  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:此处检测到错误的参数匹配器:

                  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 show up because you use argument matchers with 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 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.

                  编辑 1:

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

                  But if i mark the bundlePluginCollectionShouldNotBeNull test with @Ignore annotation, then first test case passes without any 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 called with any string parameter inside the test, but not to invoke the test itself. For your test use empty string "" instead to anyString().

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

                  上一篇:用于 int 原语的 Mockito 下一篇:Mockito - 拦截模拟上的任何方法调用

                  相关文章

                  最新文章

                  <legend id='LxAhy'><style id='LxAhy'><dir id='LxAhy'><q id='LxAhy'></q></dir></style></legend>

                    • <bdo id='LxAhy'></bdo><ul id='LxAhy'></ul>
                    <i id='LxAhy'><tr id='LxAhy'><dt id='LxAhy'><q id='LxAhy'><span id='LxAhy'><b id='LxAhy'><form id='LxAhy'><ins id='LxAhy'></ins><ul id='LxAhy'></ul><sub id='LxAhy'></sub></form><legend id='LxAhy'></legend><bdo id='LxAhy'><pre id='LxAhy'><center id='LxAhy'></center></pre></bdo></b><th id='LxAhy'></th></span></q></dt></tr></i><div id='LxAhy'><tfoot id='LxAhy'></tfoot><dl id='LxAhy'><fieldset id='LxAhy'></fieldset></dl></div>

                    1. <tfoot id='LxAhy'></tfoot>

                      <small id='LxAhy'></small><noframes id='LxAhy'>