Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try catch in a JUnit test

I'm writing unit tests for an application that already exists for a long time. Some of the methods I need to test are build like this:

public void someMethod() throws Exception {     //do something  } 

If I want to test these methods I have to write something like this in my unit test:

@Test public void someTest() {    try {       someMethod();    }    catch (Exception e) {       e.printStackTrace();    } } 

Is it a good practice to do this? Or is there an other way to test these methods?

I did some research on the internet and I found a few solutions with the @Rule annotation and @Test(expected=Exception.class), but that's not working (Eclipse keeps showing the someMethod() line in the test as wrong). I don't know if these are good solutions, because I'm pretty new to the whole unit testing story.

If someone who knows a lot about this could help me out, I would be really thankful.

like image 490
Nelsch Avatar asked Jul 15 '15 07:07

Nelsch


People also ask

Can we write try-catch in JUnit?

You can just let take JUnit to take care of the Exception by adding it to your method sig: public void someTest() throws Exception. However if you want to catch the Exception yourself to assert it being caugt the example you have given is good to go.

How do you deal with try-catch in JUnit?

As you can see, we use the fail() statement at the end of the catch block so if the code doesn't throw any exception, the test fails. And we catch the expected exception by the catch clause, in which we use assertEquals() methods to assert the exception message. You can use this structure to test any exceptions.

How do you throw an exception in JUnit?

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.

How does JUnit 4 handle exceptions?

JUnit provides an option of tracing the exception handling of code. You can test whether the code throws a desired exception or not. The expected parameter is used along with @Test annotation. Let us see @Test(expected) in action.


1 Answers

Since Exception is a checked exception, you either:

  • Have to catch the exception in a try...catch statement, or
  • Declare the exception to be thrown in the method itself.

What you have up there works fine, but my personal preference is to declare the exception to be thrown. This way, if an exception I'm not expecting is thrown during the run of the test, the test will fail.

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

If we need to see if a specific exception is thrown, then you have the option of using @Rule or adding the value to the @Test annotation directly.

@Test(expected = FileNotFoundException.class) public void someTest() throws Exception {     // dodgy code here } 

In JUnit 5, you can leverage Assertions.assertThrows to accomplish the same thing. I'm less familiar with this overall since it's not yet GA at the time of editing, but it appears to accept an Executable coming from JUnit 5.

@Test public void someTest() {     assertThrows(FileNotFoundException.class, () ->          { dodgyService.breakableMethod() }; } 
like image 126
Makoto Avatar answered Oct 15 '22 11:10

Makoto