gpt4 book ai didi

java - PowerMock + TestNg | PowerMock + TestNg | PowerMock + TestNg预期异常不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:28 29 4
gpt4 key购买 nike

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

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

最佳答案

当我们期望 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();
}

关于java - PowerMock + TestNg | PowerMock + TestNg | PowerMock + TestNg预期异常不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29092935/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com