How do I deal with exception thrown by a try/catch statement in Java code? What is the best way to test with Junit such a scenario?
This is the code I am trying to work with, any improvement is most welcome:
try {
sessionObj = buildSessionFactory().openSession();
sessionObj.getTransaction().commit();
return true;
} catch(Exception sqlException) {
if(null != sessionObj.getTransaction()) {
sessionObj.getTransaction().rollback();
}
return false;
}
Junit code:
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void throwsExceptionWithSpecificTypeAndMessage() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("sqlException");
throw new IllegalArgumentException("sqlException");
}