Java PowerMock+;TestNg |预期异常不起作用

Java PowerMock+;TestNg |预期异常不起作用,java,unit-testing,testng,powermock,Java,Unit Testing,Testng,Powermock,我正在尝试编写一个TestNg测试用例。这是为了测试当客户端抛出异常时会发生什么。我正在使用PowerMock模拟客户端调用。下面是我的测试方法(仅截取了UnitTest,而不是正在测试的代码) @Test(expectedExceptions={ExecutionException.class}) public void handleExceptionTest() throws Exception { // Set Client Expectations LLCCli

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

    @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})

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

如果我们期望Java SE核心库中不存在异常,那么就不可能使用TestNG+PowerMockito+expectedException注释。换句话说,如果期望exception.class或属于Java SE API的异常,就不会有问题但当创建新异常(即使从exception类继承)时,或者使用了来自JavaEE或任何商业库的异常时,它将不起作用

这将通过:
@Test(expectedExceptions=NullPointerException.class)
public void testMethod()引发异常{
抛出新的NullPointerException();
}

这不是(已创建MyExcetpion):
@Test(expectedExceptions=MyException.class)
public void testMethod()引发异常{
抛出新的MyException();
}

这两者都不是(WebApplicationException属于javax.ws.rs-JavaEE):
@Test(expectedExceptions=WebApplicationException.class)
public void testMethod()引发异常{
抛出新的WebApplicationException();

}

我也尝试过
@Test(expectedExceptions={TestException.class})
但测试用例仍然失败请展示您的代码。@Jens添加了unittest代码,但没有测试的代码,因为我无法在这里共享。如果这无助于回答我的问题,我可以用工作示例重现问题。是的,请发布一个示例。