我有一个带有 2 个模拟的测试。我用一些 when
调用准备第一个 mock,然后用 when
调用准备第二个 mock,它将返回第一个 mock。
但是,我在准备第二个模拟的行中收到了 InvalidUseOfMatchersException
。 Mockito 似乎不喜欢使用 any(HttpRequest.class)
。我在其他项目中多次使用这种方法,没有问题。这是什么原因?
我考虑过的导致此错误的一些可能原因包括
- 该项目使用 Java 6。
mockito-core
版本为 1.8.5。 getResponse
方法仅在SimpleHttpResponseProvider
的父类(super class)中定义。getResponse
在父类(super class)中被标记为final
。如果使用反射,我不确定这会给 Mockito 带来问题。 编辑:是的,这就是问题所在。getResponse
方法是同步的
。但是,删除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)
最佳答案
问题与匹配器无关。这是因为我试图模拟的方法在父类(super class)中被定义为 final
。删除 final
关键字后,错误消失。
关于java - Mockito: "Invalid use of argument matchers!",但我已经一直在使用匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58480658/