@Test(expectedExceptions=DataAccessException.class) public void testUpdateSubModuleOrderDateExceptionCheck() { //some code to initialize// UserSubModuleDao userSubModuleDao = mock(UserSubModuleDao.class); userModuleServiceImpl.setUserSubModuleDao(userSubModuleDao); UserSubModule userSubModule=new UserSubModule(); UserSubModuleId userSubModuleId=new UserSubModuleId(); when(userSubModuleDao.findById(any(UserSubModuleId.class),eq(false))).thenThrow(DataAccessException.class); userModuleServiceImpl.updateSubModuleOrder(data, moduleSysId, userId);
I want to throw the Db exception for code coverage . its working if i give expected exception as : Exception.class
but not for DataAccessException.class
My method in original class is as following:
public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) { try { for (int i = 0; i < data.length; i++) { SubModule subModule=new SubModule(); subModule.setSubModuleId(data[i]); UserSubModuleId userSubModuleId = new UserSubModuleId(); userSubModuleId.setSubModuleId(subModule); userSubModuleId.setUserId(userId); userSubModuleId.setUserModuleId(moduleSysId); UserSubModule userSubmodule = new UserSubModule(); userSubmodule = userSubModuleDao.findById(userSubModuleId, false); catch (DataAccessException ewmsDataExp) { LOGGER.error( "Database Exception while updateSubModuleOrder because of {}", ewmsDataExp.getMessage()); throw new EWMSServiceException( "Database Exception while updateSubModuleOrder" + ewmsDataExp.getMessage()); } catch (Exception exp) { LOGGER.error( "System Exception while updateSubModuleOrder because of {}", exp.getMessage()); throw new EWMSServiceException( "Database Exception while updateSubModuleOrder" + exp.getMessage()); }*
i get the error
FAILED: testUpdateSubModuleOrderDateExceptionCheck org.testng.TestException: **Expected exception org.springframework.dao.DataAccessException but got org.testng.TestException:** **Expected exception org.springframework.dao.DataAccessException but got java.lang.InstantiationError: org.springframework.dao.DataAccessException** at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 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:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) 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)
Default test
===============================================
===============================================
When using JUnit 4, we can simply use the expected attribute of the @Test annotation to declare that we expect an exception to be thrown anywhere in the annotated test method. In this example, we've declared that we're expecting our test code to result in a NullPointerException.
In JUnit 5, to write the test code that is expected to throw an exception, we should use Assertions. assertThrows(). In the given test, the test code is expected to throw an exception of type ApplicationException or its subtype. Note that in JUnit 4, we needed to use @Test(expected = NullPointerException.
Master Java Unit testing with Spring Boot and MockitoMockito provides the capability to a mock to throw exceptions, so exception handling can be tested. Take a look at the following code snippet. //add the behavior to throw exception doThrow(new Runtime Exception("divide operation not implemented")) .
Checked exception (compile time exception)If the compiler does not see a try or catch block or throws keyword to handle a checked exception, it throws a compilation error. Checked exceptions are generally caused by faults outside code like missing files, invalid class names, and networking errors.
Change this:
thenThrow(DataAccessException.class)
to
thenThrow(new DataAccessException("..."){ })
Example:
when(userSubModuleDao.findById(any(UserSubModuleId.class),eq(false))).thenThrow(new DataAccessException("..."){});
You can only pass a Class reference when that Exception type has a No-Arg constructor, and the Spring exception does not have one.
Try
Mockito.doThrow(new Exception()).when(mockedObject).methodName(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With