How do you assert that a certain exception is thrown in JUnit 4 tests?

junit assert exception message
junit 4 assert exception
how to write test case for catch (exception in junit)
junit 5 assert exception message
junit assert no exception
junit test case for custom exception
hamcrest assert exception
junit 5 test for exception thrown

How can I use JUnit4 idiomatically to test that some code throws an exception?

While I can certainly do something like this:

public void testFooThrowsIndexOutOfBoundsException() {
  boolean thrown = false;

  try {
  } catch (IndexOutOfBoundsException e) {
    thrown = true;


I recall that there is an annotation or an or something that is far less kludgy and far more in-the-spirit of JUnit for these sorts of situations.

There are two ways of writing test case

  1. Annotate the test with the exception which is thrown by the method. Something like this @Test(expected = IndexOutOfBoundsException.class)
  2. You can simply catch the exception in the test class using the try catch block and assert on the message that is thrown from the method in test class.

    catch(exception to be thrown from method e)
         assertEquals("message", e.getmessage());

I hope this answers your query Happy learning...

How do you assert that a certain exception is thrown in JUnit 4 tests , It depends on the JUnit version and what assert libraries you use. For JUnit5 and 4.13 see answer; If you use  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. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

I wanted to comment with my solution to this problem, which avoided needing any of the exception related JUnit code.

I used assertTrue(boolean) combined with try/catch to look for my expected exception to be thrown. Here's an example:

public void testConstructor() {
    boolean expectedExceptionThrown;
    try {
        // Call constructor with bad arguments
        double a = 1;
        double b = 2;
        double c = a + b; // In my example, this is an invalid option for c
        new Triangle(a, b, c);
        expectedExceptionThrown = false; // because it successfully constructed the object
    catch(IllegalArgumentException e) {
        expectedExceptionThrown = true; // because I'm in this catch block
    catch(Exception e) {
        expectedExceptionThrown = false; // because it threw an exception but not the one expected

Exception Testing, Contribute to junit-team/junit4 development by creating an account on GitHub. The method assertThrows has been added to the Assert class in version 4.13. reference) results in a particular type of exception being thrown. If the expected exception is thrown, assertThrows returns the exception, which enables us to also assert on the message. Furthermore, it's important to note that this assertion is satisfied when the enclosed code throws an exception of type NullPointerException or any of its derived types.

try {
    my method();
    fail( "This method must thrwo" );
} catch (Exception ex) {

Mockito How to mock and assert a thrown exception?, For JUnit 4 you can use this: @Test(expected = IndexOutOfBoundsException.​class). public void testIndexOutOfBoundsException(). {. ArrayList  thrown = true; } assertTrue(thrown); } I recall that there is an annotation or an or something that is far less kludgy and far more in-the-spirit of JUnit for these sorts of situations.

Exception testing · junit-team/junit4 Wiki · GitHub, Java code examples for testing exceptions with JUnit. class implements a test method that asserts IllegalArgumentException is thrown by the  In JUnit 4 or later you can test the exceptions as follows @Rule public ExpectedException exceptions = ExpectedException.none(); this provides a lot of features which can be used to our JUnit tests. If you see the below example I am testing 3 things on the exception. The Type of exception thrown; The exception Message; The cause of the exception

JUnit Test Exception Examples, We can use JUnit 4 @Test annotation expected attribute to define the expected exception thrown by the test method. @Test(expected = Exception.class) public  With this method you can assert that a given function call (specified, for instance, as a lambda expression or method reference) results in a particular type of exception being thrown. In addition it returns the exception that was thrown, so that further assertions can be made (e.g. to verify that the message and cause are correct).

JUnit Assert Exception - JUnit 5 and JUnit 4, How can we use JUnit 4 to test whether some code throws an Exception or not? If my test method throws a checked exception and if I want to assert the message along with the exception, is there a way to do so with JUnit @Test annotation? AFAIK, JUnit 4.7 doesn't provide this feature but does any future versions provide it? I know in .NET you can assert the message and the exception class.

  • The problem with any other approach but this is that they invariably end the test once the exception has been thrown. I, on the other hand, often still want to call org.mockito.Mockito.verify with various parameters to make sure that certain things happened (such that a logger service was called with the correct parameters) before the exception was thrown.
  • You can see how to exceptions test in JUnit wiki page
  • @ZeroOne - For that I would have two different tests- one for the exception and one to verify interaction with your mock.
  • There is a way to do this with JUnit 5, I have updated my answer below.
  • The second catch would swallow the stack trace if some other exception is thrown, losing useful information
  • You are answering on a 10 year old question, please expand your answer, why you think it provides something new, that the accepted answer does not cover. Also always try to tell something about your code and provide documentation or explanation.
  • This is basically a duplicate of this 10 year old answer (except for the check for the error message).
  • I am not sure if the test depending just on the error message is a good practice.
  • Yeah it is a good practice and useful when you have many error messages, so that you make sure that this specific one will be thrown (many possible exceptions)
  • Please what do you mean by expand answer Hebrow, this answer is for the question, so if you understand well the question and the code on it so the answer will be straightforward, he is needing just some code