I am new to java, Junit and jacoco. I am doing exception handling as below. I am catching Exception class as well in case the method throws any other exception that were missed to catch earlier.
private void myMethod1() throws MyCustomException {
....
try {
..... straight JDBC calls for select, insert, update operations ....
} catch (SQLException sqEx) {
logger.error(....);
new MyCustomException(.....);
} catch (RuntimeException rEx) {
logger.error(....);
new MyCustomException(.....);
} catch (Exception ex) {
logger.error(....);
new MyCustomException(.....);
}
}
in Junit test, tried below. When I have runtime any exception I throw is always going to RuntimeException catch block itself unless I throw Exception. which other checked exception can I try so it goes into Exception catch block. Due to this I am unable to get the code coverage needed.
private void testMyMethod1() {
....
try {
.....
when(...).thenThrow(new SocketException());
spy.myMethod1();
} catch (SQLException sqEx) {
assertTrue(false);
} catch (RuntimeException rEx) {
assertTrue(false)
} catch (Exception ex) {
assertTrue(true);
}
}
your help is appreciated.