Junit4: @RunWith (MyCustomRunner.class) - IllegalAccessException

Созданный на 5 дек. 2018  ·  3Комментарии  ·  Источник: junit-team/junit4

Привет,

Я попытался реализовать несколько типов "пользовательских бегунов", чтобы познакомиться с концепцией JUnit Custom Runners,
но каждый из них дает одно и то же исключение, и я слишком глуп, чтобы понять, почему.

Я написал следующий тест:

@RunWith(MyYetAnotherRunner.class)
public class BestPracticesForUsingJUnit_OwnRunner2 {
    <strong i="9">@Test</strong>
    public void one() {
    }
    <strong i="10">@Test</strong>
    public void two() {
    }
    <strong i="11">@Test</strong>
    public void three() {
    }
}

С классом "пользовательского бегуна" следующим образом:

class MyYetAnotherRunner extends Runner {
    private Class<?> testClass;
    public MyYetAnotherRunner(Class<?> testClass) {
        super();
        this.testClass = testClass;
    }
    <strong i="15">@Override</strong>
    public Description getDescription() {
        return Description.createTestDescription(testClass, "My runner description");
    }
    <strong i="16">@Override</strong>
    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);
        }
    }
}

Я тоже попытал счастья, расширив с BlockJUnit4ClassRunner , но безрезультатно:

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;
    }
}

Я запускаю тесты внутри eclipse, щелкая правой кнопкой мыши на Test-Class BestPracticesForUsingJUnit_OwnRunner2 -> Запуск от имени -> JUnit Test.

В каждом случае трассировка стека ошибок:

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)

Странно то, что с помощью Pre -formed Parameter.class -JUnit-Test-Runner все работает.

Что случилось?

Это что-то с затмением?

Самый полезный комментарий

Совершенно не беспокойтесь!

Все 3 Комментарий

Класс бегуна должен быть public .

извините за то, что открываю билет и беспокою вас такими банальными вещами.

Пусть этот тикет, который даже не достоин быть в закрытой секции тикетов, исчезнет в недрах репозитория JUnit-Github.

Совершенно не беспокойтесь!

Была ли эта страница полезной?
0 / 5 - 0 рейтинги