Mockito拒绝抛出检查异常

时间:2011-07-11 09:10:06

标签: java exception junit mockito

我正在使用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();
} 

这是抛出异常的方法:

protected MocFsm getSs7Fsm() throws FsmInitializationException {
    mocFsm.setContextProvider(getMocLocalObject());
    return mocFsm;
}  

我在测试执行期间得到的错误如下所示:

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$1.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)  

有没有人知道为什么它在这种情况下不起作用?

4 个答案:

答案 0 :(得分:8)

我终于找到了解决这个问题的方法。

我们使用了一个扩展原始Sbb类的匿名类,该类已经过测试,并且在此扩展类中实现了上述方法,并且删除了throws表达式,这导致了问题。

我应该更仔细地阅读我的同事代码。

答案 1 :(得分:3)

我觉得解决方案是使用doThrow()语法而不是when()。thenThrow()。间谍时,使用doThrow / doReturn语法进行存根更安全。原因在文档中有所描述:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#13

我认为我们可以在此实例中改进异常消息,使其为m

答案 2 :(得分:1)

遵循this answer。使用 Mockito 的 doAnswer 而不是 thenThrow 方法解决了我的问题。

Mockito.doAnswer(
      invocation -> {
        throw new Exception("Something went wrong");
      })
  .when(moc)
  .getSs7Fsm();

我知道为时已晚,但以防万一有人面临同样的问题。

答案 3 :(得分:0)

我知道这篇文章很老但我的解决方案可能对某人有所帮助: 我通过将我的方法的可见性更改为公共来解决了同样的错误。

在这种情况下,你应该有改变

protected MocFsm getSs7Fsm() throws FsmInitializationException {
    ...
}

public MocFsm getSs7Fsm() throws FsmInitializationException {
    ...
}