从SpringJUnit4ClassRunner子类获取ApplicationContext

时间:2019-05-09 16:04:50

标签: spring inheritance dependency-injection applicationcontext spring-junit

我有以下课程:

public class MyJunitRunner extends SpringJUnit4ClassRunner {
    public MyJunitRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    public void run(RunNotifier notifier){
        ApplicationContext applicationContext = this.getTestContextManager().getTestContext().getApplicationContext();
        MyJunitRunListener myJunitRunListener = new MyJunitRunListener(applicationContext);
        notifier.addListener(myJunitRunListener);
        notifier.fireTestRunStarted(getDescription());
        super.run(notifier);
    }
}

但是,由于尝试获取ApplicationContext时出现以下错误,因此无法编译:

  

类型为TestContextManager的方法getTestContext()不是   可见

在TestContextManager的内部,我看到以下方法声明:

protected final TestContext getTestContext() {
    return this.testContext;
}

您知道为什么我无法访问getTestContext()方法吗?

是否可以通过其他方式访问ApplicationContext?

0 个答案:

没有答案