Junit’s annotation based expected exception testing

Junit provides an option of tracing exception thrown by a Java method. There are different ways of handling exceptions in JUnit tests. Standard Junit’s org.junit.Test annotation offers expected attribute that allows us specifying the a Throwable to cause a test method to succeed if an exception of the specified class is thrown by the method. A Java unit test should verify correct exception thrown in exceptional case and no exception should be thrown in normal case. For this to test exception thrown by a Java method using @Test(expected=””) we need to use at least Junit 4.7. One of the many situations where we need to test exception thrown by a Java method is testing an API method that should throw IllegalArgumentException if arguments passed to the method are not matching to pre-conditions. For example, in order to test that we need to use @Test(expected=IllegalArgumentException.class) annotation. We can replace IllegalArgumentException.class with any other exception e.g. NullPointerException.class or ArithmeticException.class etc as per the requirment. In order to test any Java method for throwing excpetion, we need to ensure that arguments provided to the method, from the test must result in expected Exception, otherwise JUnit test will fail.

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven installed and configured
Junit dependency in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : java

Step 2. Modify the pom.xml file as shown below.

Step 3. Here is an example of how to test exception thrown by a method called secsToMins(), which returns minutes as seconds/60, but before calculating minutes for given seconds it checkes whether minutes are positive or not, and if minutes are zero or negtaive it throws IllegalArgumentException.

Step 4. Below is the example for Exception testing, we can see that our testSecsToMins() method is annotated with @Test(expected=IllegalArgumentException.class), which means it expect an illegalArgumentException, when we run this JUnit test.

Step 5. Now if you run the test class test will be pass with current arguments, but if we change arguments test will be failed like below, because it would not get IllegalArgumentException any more.

That’s all on how to test expected exception in JUnit4. It is an very easy to test Java method with Junit’s annotation based approach. Now we can easily verify any Java method for both correct and incorrect set of inputs, along with exceptions for normal and exceptional cases.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.