Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java how can I validate a thrown exception with JUnit?

When writing unit tests for a Java API there may be circumstances where you want to perform more detailed validation of an exception. I.e. more than is offered by the @test annotation offered by JUnit.

For example, consider an class that should catch an exception from some other Interface, wrap that exception and throw the wrapped exception. You may want to verify:

  1. The exact method call that throws the wrapped exception.
  2. That the wrapper exception has the original exception as its cause.
  3. The message of the wrapper exception.

The main point here is that you want to be perf additional validation of an exception in a unit test (not a debate about whether you should verify things like the exception message).

What's a good approach for this?

like image 620
Iain Avatar asked Apr 24 '09 12:04

Iain


People also ask

How do I validate exceptions 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 can you use JUnit for testing a code that throws a desired exception?

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.

How do I validate exceptions in JUnit 5?

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.

How do you test the exception thrown by the following method in JUnit?

In order to test the exception thrown by any method in JUnit 4, you need to use @Test(expected=IllegalArgumentException. class) annotation. You can replace IllegalArgumentException. class with any other exception e.g. NullPointerException.


1 Answers

In JUnit 4 it can be easily done using ExpectedException rule.

Here is example from javadocs:

// These tests all pass. public static class HasExpectedException {     @Rule     public ExpectedException thrown = ExpectedException.none();      @Test     public void throwsNothing() {         // no exception expected, none thrown: passes.     }      @Test     public void throwsNullPointerException() {         thrown.expect(NullPointerException.class);         throw new NullPointerException();     }      @Test     public void throwsNullPointerExceptionWithMessage() {         thrown.expect(NullPointerException.class);         thrown.expectMessage("happened?");         thrown.expectMessage(startsWith("What"));         throw new NullPointerException("What happened?");     } } 
like image 176
Jonas Avatar answered Sep 18 '22 21:09

Jonas