Testing for exceptions

Testing for exceptions


Posted in : Java Posted on : November 24, 2010 at 7:02 PM Comments : [ 0 ]

This section contains the detail about Testing for exceptions

Testing for exceptions in JUnit

The Test annotation supports 'expected' parameters which declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails. For example, the following test succeeds :

package devmanuals.junit;

import java.util.ArrayList;

import org.junit.Test;

public class ExceptionTest {
@Test(expected = IndexOutOfBoundsException.class)
public void outOfBounds() {
new ArrayList<Object>().get(1);
}
}

Output

Since it throws the expected exception that's why test is successful :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics