Java Mockito 拒绝抛出受检异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6647957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito refuses to throw checked exception
提问by Edheene
I'm using Mockito for my unit tests and I encounetered a problem with throwing exception on spied object. I have done this before on this object (in different test case and it worked). Here's the test code:
我正在使用 Mockito 进行单元测试,并且遇到了在监视对象上抛出异常的问题。我之前在这个对象上做过这个(在不同的测试用例中并且它有效)。这是测试代码:
@Test
public void callInitiated_FsmInitializationException() throws FsmInitializationException, Exception {
MocSbb moc = Mockito.spy(testObj);
MocTracer tracer = Mockito.mock(MocTracer.class);
Mockito.doReturn(tracer).when(moc).getTracer();
CAP2InitialDPArg idp = Mockito.mock(CAP2InitialDPArg.class);
MocFsm mocFsm = Mockito.mock(MocFsm.class);
//Mockito.doReturn(mocFsm).when(moc).getSs7Fsm();
TicketingLocalInterface ticketing = mockTicketingLocalInterface();
CAP2InitialDPArgWrap idpWrap = Mockito.mock(CAP2InitialDPArgWrap.class);
PowerMockito.whenNew(CAP2InitialDPArgWrap.class).withArguments(idp, tracer).thenReturn(idpWrap);
MocSession mocSession = Mockito.mock(MocSession.class);
Mockito.doReturn(mocSession).when(moc).getMocSession();
Mockito.when(moc.getSs7Fsm()).thenThrow(new FsmInitializationException()); ////HERE'S THE PROBLEM
moc.callInitiated(idp);
verify(moc).getFailedFsm();
}
here's the method on which the exception should be thrown:
这是应该抛出异常的方法:
protected MocFsm getSs7Fsm() throws FsmInitializationException {
mocFsm.setContextProvider(getMocLocalObject());
return mocFsm;
}
the error I get during test execution looks like this:
我在测试执行期间得到的错误如下所示:
Testcase: callInitiated_FsmInitializationException(com.nsn.as.ccs.moc.sbb.MocSbbTest): Caused an ERROR
Checked exception is invalid for this method!
Invalid: com.nsn.as.ccs.moc.fsm.FsmInitializationException
org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: com.nsn.as.ccs.moc.fsm.FsmInitializationException
at com.nsn.as.ccs.moc.sbb.MocSbbTest.callInitiated_FsmInitializationException(MocSbbTest.java:1194)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:322)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:309)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:297)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
Does anyone have any idea why it doesn't work in this case?
有谁知道为什么在这种情况下它不起作用?
采纳答案by Edheene
I have finally found the solution for this problem.
我终于找到了这个问题的解决方案。
We have used an anonymous class extending original Sbb
class which was tested and in this extended class implementation of mentioned method was altered and throws
expression was removed which caused the problem.
我们使用了一个匿名类扩展原始Sbb
类,该类经过测试,在这个扩展类中,提到的方法的实现被改变,throws
表达式被删除,这导致了问题。
I should have read my colleagues code more carefully.
我应该更仔细地阅读我同事的代码。
回答by Szczepan
I have a feeling the solution is to use doThrow() syntax instead of when().thenThrow(). When spying it is safer to use doThrow/doReturn syntax for stubbing. The reasons are somewhat described in the documentation: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#13
我有一种感觉,解决方案是使用 doThrow() 语法而不是 when().thenThrow()。监视时使用 doThrow/doReturn 语法进行存根更安全。原因在文档中有所描述:http: //docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#13
I think we can improve an exception message in this instance so that it is m
我认为我们可以在这种情况下改进异常消息,使其成为 m
回答by Rubén Viguera
I know this post is very old but my solution it could be of help to someone: I solved this same error by changing the visibility of my method to be test to public.
我知道这篇文章很旧,但我的解决方案可能对某人有所帮助:我通过将要测试的方法的可见性更改为公开来解决了同样的错误。
In this case you whould have change
在这种情况下,你应该有改变
protected MocFsm getSs7Fsm() throws FsmInitializationException {
...
}
to
到
public MocFsm getSs7Fsm() throws FsmInitializationException {
...
}