我在测试的类中使用hyStrix(v。1.5.6)命令,我正在尝试使用Mockito模拟它(v。1.10.19)。
最近,如果命令因超时而失败,我必须添加特殊行为:
try {
result = command.execute();
} catch (HystrixRuntimeException err) {
if (command.isResponseTimedOut()) {
/** do stuff **/
}
/** do stuff **/;
}
这是我的测试类中的代码:
HttpClientCommand command = mock(HttpClientCommand.class);
when(commandFactory.get(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))).thenReturn(command);
when(command.execute()).thenThrow(hystrixRuntimeException);
when(command.isResponseTimedOut()).thenReturn(false);
我的HttpClientCommand
扩展了HystrixCommand
类。execute
方法在此类中,我没有问题。
然后,HystrixCommand
类扩展了isResponseTimedOut
方法所在的AbstractCommand
类。当我尝试固执时,我会从Mockito获得NPE(在when(command.isResponseTimedOut()).thenReturn(false);
行上,在调用我测试过的类中的任何方法)。
堆栈跟踪:
java.lang.Exception: Unexpected exception, expected<my.custom.exception> but was<java.lang.NullPointerException>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.NullPointerException
at com.netflix.hystrix.AbstractCommand.isResponseTimedOut(AbstractCommand.java:1809)
at com.netflix.hystrix.HystrixCommand.isResponseTimedOut(HystrixCommand.java:46)
at com.myClassTest.testGetLocation_shouldThrowCustomException_whenRequestException(myClassTest.java:300)
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:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 22 more
我该如何存根此方法?
好的,问题是由于cglib的错误,mockito 1.x使用cglib。当在非公共父母中声明模拟的方法时,CGLIB不会拦截该方法,因此执行真实代码。该问题仍然可以在我们的旧问题跟踪器上可用:问题212
两个选项:
-
在
HttpHystrixCommand
中写一个将调用超级方法的公共过载,但是由于Abstract命令具有很多处理失败的方法,因此这不是一个可以很好地扩展的选项。 -
使用Mockito 2.我们已经与Bytebuddy的开发商(Rafel Winterhalter)合作,以取代CGLIB。Bytebuddy没有CGLIB的缺点,几年来几乎被放弃了。当前版本为2.2.29,并且在此期间发布常规更新时。