2

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");
}
P.M
  • 2,880
  • 3
  • 43
  • 53
jhon
  • 21
  • 3
  • your test doesn't test anything. Also, your method won't throw an Exception, so your test makes little sense. – Stultuske Dec 17 '19 at 09:42
  • I think this stacklink might help you: https://stackoverflow.com/questions/31423643/try-catch-in-a-junit-test – Quadrivics Dec 17 '19 at 09:42
  • 1
    @Quadrivics how so? the method won't throw an Exception. – Stultuske Dec 17 '19 at 09:43
  • I think one should not only judge the question to see if it's plausible or not, but also try to inform the person about the answer to his question even if you think the question is not valid. Apparantly he misses knowledge about testing try catch statements, and in the link provide by me i found a good explanation of it all. – Quadrivics Dec 17 '19 at 09:45
  • @Quadrivics No, you don't. what is explained there, is how to test exceptions thrown by the method that is to test. That doesn't occur here. – Stultuske Dec 17 '19 at 09:47
  • @Jhon do you know how to use mocks? – Stultuske Dec 17 '19 at 09:47
  • @Stultuske unfortunately no, but if I want to cover each branch of the code with the test, how I can do? – jhon Dec 17 '19 at 09:50
  • @jhon you will need to learn how to mock your services. Then you write a mock for the result of your buildSessionFactory() (or another part in your try block) and on calling that, you have your mock throw an(y) exception. That will land you in the catch block – Stultuske Dec 17 '19 at 09:53
  • @Test(expected = ArithmeticException.class) public void testJUnitMessage(){ – Harsh Mishra Dec 17 '19 at 10:53
  • @HarshMishra that will never work. The OP is trying to test the flow where the method enters the catch block. The method doesn't throw an Exception – Stultuske Dec 17 '19 at 11:05

1 Answers1

1

Exception is a checked exception you either have to catch the exception using try... catch block or declare the exception.

@Test
public void someTest() throws Exception {
    // your code here
}

This way we can declare the exception and Junit will print the stack trace if exception occurs.

Or

Optionally specify expected, a Throwable, to cause a test method to succeed iff an exception of the specified class is thrown by the method.

 Class<? extends Throwable> org.junit.Test.expected()

@Test(expected = someException.class)
public void someTest() throws Exception {
    // your code here
}
Seshidhar G
  • 265
  • 1
  • 9
  • 1
    You really should read both the question AND the comments already given. This doesn't answer the OP's question. What this tests, is whether the method that is being tested throws an Exception. The method that is being tested will NEVER throw an Exception, so your test would always fail. What he wants is to execute a test, where in the method he is testing, the catch block will be executed, hence, there will be an Exception thrown within the try block. – Stultuske Dec 17 '19 at 12:11