1.

What Happens If A Test Method Throws An Exception?

Answer»

If you write a TEST method that throws an exception by itself or by the method being tested, the JUnit RUNNER will DECLARE that test as FAIL.

The example test below is designed to let the test fail by throwing the uncaught IndexOutOfBoundsException exception:

import org.junit.*;
import java.util.*;
public class UnexpectedExceptionTest2 {
// throw any unexpected exception
@Test public void testGet() throws Exception {
ARRAYLIST emptyList = new ArrayList();
Exception anyException = null; // don't catch any exception
Object o = emptyList.get(1); }
}

If you write a test method that throws an exception by itself or by the method being tested, the JUnit runner will declare that test as fail.

The example test below is designed to let the test fail by throwing the uncaught IndexOutOfBoundsException exception:

import org.junit.*;
import java.util.*;
public class UnexpectedExceptionTest2 {
// throw any unexpected exception
@Test public void testGet() throws Exception {
ArrayList emptyList = new ArrayList();
Exception anyException = null; // don't catch any exception
Object o = emptyList.get(1); }
}



Discussion

No Comment Found