Nep*_*oxx 5 java junit junit4 mockito powermock
我正在使用 PowerMock (Mockito) 模拟对同一类中另一个方法的子调用。更具体地说,我有这样的事情:
public class myClass{
public void MyMethod1(){
//do something
try{
myMethod2();
} catch (MyExceptionType e) {
//do something
throw e;
}
}
public int MyMethod2() throws MyExceptionType {...}
}
Run Code Online (Sandbox Code Playgroud)
现在在我的单元测试中,我可以使用间谍来模拟 MyMethod2 的响应,并执行类似doReturn(1).when(myClass).myMethod2(). 然而,当我做这样的事情时,奇怪的事情发生了doThrow(myExeptionType).when(myClass).myMethod2():当我在测试期间调用 myClass.myMethod1() 时,它会抛出 NullPointerException,但奇怪的是,如果我使用调试器并检查throw e, e 是 MyExceptionType 类型的正确异常。
这是该 NullPointerException 的堆栈跟踪:
java.lang.NullPointerException
at java.util.Arrays$ArrayList.<init>(Arrays.java:2842)
at java.util.Arrays.asList(Arrays.java:2828)
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.filter(StackTraceFilter.java:31)
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.filter(ConditionalStackTraceFilter.java:23)
at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:29)
at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:51)
at com.amazon.inventory.workflow.common.wrapper.FCContainerServiceWrapper$$EnhancerByMockitoWithCGLIB$$a0f00456.getContainerHierarchyDown(<generated>)
at com.amazon.inventory.workflow.common.wrapper.containerservice.GetContainerHierarchyDownTest.runTest(GetContainerHierarchyDownTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
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.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
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.junit.runner.JUnitCore.run(JUnitCore.java:148)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)
希望我的问题不会太混乱,谢谢!
您的评论和随后的回答已经揭示了问题所在。您正在尝试模拟您的异常对象。Mockito 的设计初衷并不是能够做到这一点。原因是异常通常被认为是值对象。它们携带信息——消息、堆栈跟踪,有时还包含对第二个异常的引用;但作为一般规则,它们实际上没有任何功能。
模拟任何类的目的是获得一个没有任何自身功能的对象,也就是说,它的任何方法都不执行任何操作,除非在测试中显式实现。但异常已经符合该标准,因此嘲笑它没有任何好处。http://www.mockobjects.com/2007/04/test-smell-everything-is-mocked.html上的建议确实是个好建议。
因此,您有几个选择,这两种选择都可以很好地解决您的问题。
(1) 创建一个真正的异常并在测试中使用它。根据构造函数的不同MyException,这可能看起来像这样。
MyException toThrow = new MyException("testing");
doThrow(toThrow).when(someMock).someMethod();
Run Code Online (Sandbox Code Playgroud)
(2) 让 Mockito 为您创建异常对象,只需在调用中指定其类即可doThrow。
doThrow(MyException.class).when(someMock).someMethod();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18804 次 |
| 最近记录: |