The problem is that junitTest2 is nested inside the exceptionTesting exception. You have two options for solving the problem:
1) Get rid of the inner class having your tests in the top level class:
import org.junit.*; import org.junit.Test; public class exceptionTesting { @Test (expected = ArithmeticException.class) public void divisionWithException(){ int i = 1/0; } }
2) Let your top-level class indicate that the tests are wrapped, and make your inner class static:
import org.junit.*; import org.junit.Test; @RunWith(Enclosed.class) public class exceptionTesting { public static class junitTest2{ @Test (expected = ArithmeticException.class) public void divisionWithException(){ int i = 1/0; } } }
Unless you have a reason for the inner class, the best option is # 1. Also note that in Java, the convention should start with classes with uppercase names. Thus, exceptionTesting will become ExceptionTesting.
source share