Reputation: 125
I am trying to write a TestNg test case. This is to test what happens when a client throws an exception. I am using PowerMock to mock the client call. Below is my test method (only the UnitTest Snipped and not the code being tested
@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();
}
Though the mock throws the correct exception, TestNG fails the test case with the below output:
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
The test case works properly if is set the below expectations:
@Test(expectedExceptions={ExecutionException.class})
But I don't want to do it as it beats the purpose of the test.
Upvotes: 1
Views: 838
Reputation: 21
There is no possibility to use TestNG + PowerMockito + expectedException annotation in case when we expect exception that doeasn't exist in Java SE Core library. In another words, if Exception.class is expected or exception that belongs to Java SE API, there will be no problem. But when new exception is created (even if inherited from Exception class), or there is used an exception from Java EE or any commercial library, it will not work.
This will pass:
@Test(expectedExceptions = NullPointerException.class)
public void testMethod() throws Exception {
throw new NullPointerException();
}
This not (MyExcetpion was created):
@Test(expectedExceptions = MyException.class)
public void testMethod() throws Exception {
throw new MyException();
}
This neither (WebApplicationException belongs to javax.ws.rs - Java EE):
@Test(expectedExceptions = WebApplicationException.class)
public void testMethod() throws Exception {
throw new WebApplicationException();
}
Upvotes: 2