Test cases in inner classes with JUnit
You should annontate your class with @RunWith(Enclosed.class)
, and like others said, declare the inner classes as static:
@RunWith(Enclosed.class)
public class DogTests
{
public static class BarkTests
{
@Test
public void quietBark_IsAtLeastAudible() { }
@Test
public void loudBark_ScaresAveragePerson() { }
}
public static class EatTests
{
@Test
public void normalFood_IsEaten() { }
@Test
public void badFood_ThrowsFit() { }
}
}
In JUnit 5, you simply mark non-static inner classes as @Nested
:
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class DogTests {
@Nested
public class BarkTests {
@Test
public void quietBark_IsAtLeastAudible() { }
@Test
public void loudBark_ScaresAveragePerson() { }
}
@Nested
public class EatTests {
@Test
public void normalFood_IsEaten() { }
@Test
public void badFood_ThrowsFit() { }
}
}
public class ServicesTest extends TestBase {
public static class TestLogon{
@Test
public void testLogonRequest() throws Exception {
//My Test Code
}
}
}
Making the inner class static works for me.