Java 莫基托:“我不知道;参数匹配器的使用无效&引用;,然而,我已经一直在使用匹配器

Java 莫基托:“我不知道;参数匹配器的使用无效&引用;,然而,我已经一直在使用匹配器,java,unit-testing,testing,mockito,Java,Unit Testing,Testing,Mockito,我有两个模拟的测试。我用一些when调用准备第一个mock,然后用when调用准备第二个mock,该调用将返回第一个mock 然而,在我准备第二个模拟的那一行,我得到了一个InvalidUseOfMatchersException。Mockito似乎不喜欢使用any(HttpRequest.class)。我在其他项目中多次使用这种方法,没有任何问题。原因是什么 我考虑过的这个错误的一些可能原因包括 这个项目使用Java6。mockito core的版本是1.8.5 getResponse方法仅

我有两个模拟的测试。我用一些
when
调用准备第一个mock,然后用
when
调用准备第二个mock,该调用将返回第一个mock

然而,在我准备第二个模拟的那一行,我得到了一个
InvalidUseOfMatchersException
。Mockito似乎不喜欢使用
any(HttpRequest.class)
。我在其他项目中多次使用这种方法,没有任何问题。原因是什么

我考虑过的这个错误的一些可能原因包括

  • 这个项目使用Java6。mockito core的
    版本是1.8.5
  • getResponse
    方法仅在
    SimpleHttpResponseProvider
    的超类中定义
  • 超类中的
    getResponse
    标记为
    final
    。如果Mockito使用反射,我不确定这是否会给它带来问题编辑:是的,这就是问题所在。
  • getResponse
    方法是
    synchronized
    。但是,删除
    synchronized
    关键字并重试会导致相同的失败

    public class Team5MockHttpServerTest {
    
        private HttpResponse httpResponse;
        private SimpleHttpResponseProvider simpleHttpResponseProvider;
    
        @Test
        public void whenAllBehaviorIsNominalThenExpectationsAreMet() throws IOException {
    
            int expectedStatusCode = 200;
            String contentType = "application/json";
            String body = "{\"message\":\"Hello world\"}";
    
            this.httpResponse = mock(HttpResponse.class);
            when(this.httpResponse.getHttpCode()).thenReturn(expectedStatusCode);
            when(this.httpResponse.getContentType()).thenReturn(contentType);
            when(this.httpResponse.getContent()).thenReturn(body.getBytes());
    
            this.simpleHttpResponseProvider = mock(SimpleHttpResponseProvider.class);
            when(this.simpleHttpResponseProvider.getResponse(any(HttpRequest.class))) // Exception here
                .thenReturn(this.httpResponse);
        }
    }
    
错误:

Running com.github.kristofa.test.http.Team5MockHttpServerTest
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.05 sec <<< FAILURE!
whenAllBehaviorIsNominalThenExpectationsAreMet(com.github.kristofa.test.http.Team5MockHttpServerTest)  Time elapsed: 0.048 sec  <<< ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
0 matchers expected, 1 recorded.
This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
    at com.github.kristofa.test.http.AbstractHttpResponseProvider.getResponse(AbstractHttpResponseProvider.java:77)
    at com.github.kristofa.test.http.Team5MockHttpServerTest.whenAllBehaviorIsNominalThenExpectationsAreMet(Team5MockHttpServerTest.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
whenAllBehaviorIsNominalThenExpectationsAreMet(com.github.kristofa.test.http.Team5MockHttpServerTest)  Time elapsed: 0.049 sec  <<< ERROR!
java.lang.NullPointerException
    at com.github.kristofa.test.http.Team5MockHttpServerTest.tearDown(Team5MockHttpServerTest.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
运行com.github.kristofa.test.http.Team5MockHttpServerTest

测试运行:2,失败:0,错误:2,跳过:0,经过的时间:0.05秒问题与匹配器无关。这是因为我试图模拟的方法在超类中被定义为
final
。删除
final
关键字后,错误消失。

当(this.simpleHttpResponseProvider.getResponse(any())。然后返回(this.httpResponse)时,是否尝试删除“HttpRequest.class”;你能再检查一下你要导入的
匹配器吗?有时,有两个相互冲突的库提供
任何
匹配器。确保你使用的是来自Mockito@yashsugandh:是的,我试过了。除非我强制转换
any()
,如
(HttpRequest)any()
,否则它无法编译。如果我这样做,我会得到相同的
InvalidUseOfMatchersException
@stefanzhelayazkov:any
导入的
any
是来自
org.mockito.Matchers.any
的静态导入。