junit-team/junit — pull request #764 by ferstl (2013-11-15 10:23:44+00:00)
Title: Verify that all (base-) classes declaring @ClassRules are public

An `IllegalAccessException` is thrown in case a test class derives from a non-public base class which declares a `@ClassRule`.

For example, this code ...

class BaseTest {
@ClassRule
public static ExpectedException expEx = ExpectedException.none();
}

public class RealTest extends BaseTest {
@Test
public void myTest() {
...
}
}

… will cause this exception

java.lang.RuntimeException: How did getFields return a field we couldn't access?
at org.junit.runners.model.TestClass.getAnnotatedFieldValues(TestClass.java:161)
...
Caused by: java.lang.IllegalAccessException: Class org.junit.runners.model.FrameworkField can not access a member of class BaseTest with modifiers "public static final"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
...

To solve this issue, I added an additional verification to `RuleFieldValidator` that checks that all `@ClassRule`s are declared in public classes. A similar validation is already done for `@BeforeClass` and `@AfterClass` methods.