JUnit Test Expected Exception Example

When writing a JUnit test there are different ways to test for expected exception and below I am going to share with you three different ways.

Using @Test Expected Attribute

One way to test for expected exceptions is to use the @Test’s expected attribute to specify that the method below should throw an exception specified by the attribute. For example:

@Test( expected = NullPointerException.class )

I have created a very simple method that if run should throw a NullPointerException and the line above with the @Test( expected = ) attribute will make the test case pass.

package myunittests;

import org.junit.Test;

 
public class ExpectingExceptionTest {
    
    @Test(expected = NullPointerException.class)
    public void testNullPointerException()
    {
        String name = getName();
        System.out.println(name.length());
    }
    
    private String getName()
    {
        return null;
    }
}

Using the Try and Catch

When using the Try and Catch approach, you will need to catch the exception you are expecting first and then, inside of the catch{} block assert that the exception object is of the type you are expecting or its getMessage() method contains the message you are expecting.

Also, you will need to add the call to fail() inside of the try{} block. This is needed to make sure the test fails if the code does not throw an Exception your expect.

@Test
public void testWithTryAndCatchNullPointerException() {
    String name = getName();
    try {
        System.out.println(name.length());
        fail();
    } catch (NullPointerException ex) {
        assertTrue(ex instanceof NullPointerException);
    }
}

Using the @Rule ExpectedException

With the @Rule annotation we will first need to declare the ExpectedException and annotate it with @Rule. Inside of the Test method we will then simply need to specify the Exception we are expecting this method to throw using the expect() method of the ExpectedException object.

package myunittests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

 
public class ExpectingExceptionTest {
    
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    
    @Test 
    public void testUsingTheRuleNullPointerException()
    {
        thrown.expect(NullPointerException.class);
        String name = getName();
        System.out.println(name.length());
    }

    private String getName() {
        return null;
    }
}

Hope that this short blog post was helpful to you. Check out the below video courses and books that will help you learn more about testing your code.

Testing Java Code. Books.


Testing Your Code with JUnit. Video Courses.

JUnit and Mockito Crash Course

Learn how to use JUnit and Mockito and Unit Test in easy steps.


icon

Mockito Tutorial : Learn mocking with 25 Junit Examples

Learn unit testing and mocking with 25 Junit Examples


icon