【问题标题】:PowerMock + TestNg | expectedExceptions not workingPowerMock + TestNg |预期异常不起作用
【发布时间】:2015-03-17 06:49:55
【问题描述】:

我正在尝试编写一个 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();

}

尽管 mock 抛出了正确的异常,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})

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

【问题讨论】:

  • 我也试过@Test(expectedExceptions={TestException.class}) 但测试用例仍然失败
  • 请出示您的代码。
  • @Jens 添加了单元测试代码,但没有添加正在测试的代码,因为我无法在这里分享。如果这不能帮助回答我的问题,我可以使用工作示例重现该问题。
  • 是的,请发布样本。

标签: java unit-testing testng powermock


【解决方案1】:

如果我们预计 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(); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    相关资源
    最近更新 更多