У меня тест с 2-мя изданиями. Я готовлю первый макет с некоторыми when
вызовами, затем я готовлю второй макет с помощью вызова when
, который вернет первый макет.
Однако я получаю InvalidUseOfMatchersException
на линиигде я готовлю второй макет. Mockito, похоже, не любит использование any(HttpRequest.class)
. Я использовал этот подход много раз в других проектах, без проблем. В чем причина?
Некоторые возможные причины этой ошибки, которые я рассмотрел, включают
- , что этот проект использует Java 6. Версия
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)