Junit4: @RunWith(MyCustomRunner.class) - IllegalAccessException

Created on 5 Dec 2018  ·  3Comments  ·  Source: junit-team/junit4

Hi,

I have tried to implement several types of "custom runners" to get familiar with the concept of JUnit Custom Runners,
but every single one of them gives the same exception and I am too stupid to figure out why.

I have written the following test:

@RunWith(MyYetAnotherRunner.class)
public class BestPracticesForUsingJUnit_OwnRunner2 {
    @Test
    public void one() {
    }
    @Test
    public void two() {
    }
    @Test
    public void three() {
    }
}

With the "custom runner" class being as follows:

class MyYetAnotherRunner extends Runner {
    private Class<?> testClass;
    public MyYetAnotherRunner(Class<?> testClass) {
        super();
        this.testClass = testClass;
    }
    @Override
    public Description getDescription() {
        return Description.createTestDescription(testClass, "My runner description");
    }
    @Override
    public void run(RunNotifier notifier) {
        System.out.println("running the tests from MyRunner: " + testClass);
        try {
            Object testObject = testClass.newInstance();
            for (Method method : testClass.getMethods()) {
                if (method.isAnnotationPresent(Test.class)) {
                    notifier.fireTestStarted(Description.createTestDescription(testClass, method.getName()));
                    method.invoke(testObject);
                    notifier.fireTestFinished(Description.createTestDescription(testClass, method.getName()));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

I have also tried my luck by extending from BlockJUnit4ClassRunner, but of no avail either:

class MyRandomBlockJUnit4ClassRunner extends BlockJUnit4ClassRunner {
    public MyRandomBlockJUnit4ClassRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }
    protected java.util.List<org.junit.runners.model.FrameworkMethod> computeTestMethods() {
        java.util.List<org.junit.runners.model.FrameworkMethod> methods = super.computeTestMethods();
        Collections.shuffle(methods);
        return methods;
    }
}

I run the tests inside eclipse by "Right-click" on Test-Class BestPracticesForUsingJUnit_OwnRunner2 -> Run As -> JUnit Test.

In each case the error stacktrace is:

java.lang.IllegalAccessException: Class org.junit.internal.builders.AnnotatedBuilder can not access a member of class 
part2.ch9.RandomBlockJUnit4ClassRunner with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:413)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:87)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:73)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Strange thing is using the Pre-implemented Parameter.class-JUnit-Test-Runner everything works.

What is wrong?

Is it something with eclipse?

Most helpful comment

No worries at all!

All 3 comments

The runner class needs to be public.

sorry for opening a ticket and bothering you with such trivial things.

May this ticket, which is not even worthy of being in the closed ticket section, vanish in the depths of the JUnit-Github repo.

No worries at all!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

apreg picture apreg  ·  4Comments

diogoeag picture diogoeag  ·  35Comments

pbenedict picture pbenedict  ·  27Comments

akarnokd picture akarnokd  ·  27Comments

Reissner picture Reissner  ·  3Comments