PowerMock + TestNg | expectedExceptions无法正常工作

时间:2015-03-17 06:49:55

标签: java unit-testing testng powermock

我正在尝试编写一个TestNg测试用例。这是为了测试客户端抛出异常时会发生什么。我正在使用PowerMock来模拟客户端调用。下面是我的测试方法(只有UnitTest Snipped而不是被测试的代码

    @Test(expectedExceptions={ExecutionException.class})
public void handleExceptionTest() throws Exception {

    // Set Client Expectations
    LLCClient llcClientMock = PowerMock.createMock(LLCClient.class);
    LiftRestrictionActionProcessor testClass = new LiftRestrictionActionProcessor(llcClientMock);
    llcClientMock.liftRestriction(EasyMock.anyObject(BigInteger.class),
            EasyMock.anyObject(BigInteger.class), EasyMock.anyObject(String.class), EasyMock.anyBoolean());
    EasyMock.expectLastCall().andThrow(new RuntimeException());

    PowerMock.replayAll();

    //Run the test method
    testClass.process(exchangeMock);

    PowerMock.verifyAll();

}

虽然模拟抛出了正确的异常,但TestNG在测试用例中输出了以下输出:

org.testng.TestException: 
**Expected exception com.example.common.ExecutionException but got org.testng.TestException**: 
Expected exception com.example.common.ExecutionException but got com.example.common.ExecutionException: java.lang.RuntimeException
    at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1498)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.testng.TestException: 
Expected exception com.example.common.ExecutionException but got com.example.common.ExecutionException: java.lang.RuntimeException
    at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1498)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:754)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    ... 16 more
Caused by: com.example.common.ExecutionException: java.lang.RuntimeException
    at com.example.fees.actions.AbstractFeesProcessor.process(AbstractFeesProcessor.java:54)
    at com.example.fees.actions.AbstractFeesProcessorTest.handleExceptionTestAuditing(AbstractFeesProcessorTest.java:109)
    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:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    ... 18 more
Caused by: java.lang.RuntimeException
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:46)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:85)
    at $Proxy12.liftRestriction(Unknown Source)
    at com.example.fees.actions.LiftRestrictionActionProcessor.execute(LiftRestrictionActionProcessor.java:46)
    at com.example.fees.actions.AbstractFeesProcessor.process(AbstractFeesProcessor.java:44)
    ... 25 more

如果设置了以下期望,则测试用例可以正常运行:

@Test(expectedExceptions={ExecutionException.class})

但我不想这样做,因为它超出了测试的目的。

1 个答案:

答案 0 :(得分:0)

如果我们期望Java SE Core库中不存在异常,则不可能使用TestNG + PowerMockito + expectedException注释。换句话说,如果需要Exception.class或属于Java SE API的异常,则没有问题。但是当创建新异常时(即使从Exception类继承),或者使用Java EE或任何商业库中的异常,它将无法工作。

这将通过:
@Test(expectedExceptions = NullPointerException.class) public void testMethod() throws Exception { throw new NullPointerException(); }

这不是(MyExcetpion已创建):
@Test(expectedExceptions = MyException.class) public void testMethod() throws Exception { throw new MyException(); }

这两者都没有(WebApplicationException属于javax.ws.rs - Java EE): @Test(expectedExceptions = WebApplicationException.class) public void testMethod() throws Exception { throw new WebApplicationException(); }