繁体   English   中英

在TestExecutionListener类中为BeforeClass junit使用Autowired

[英]Using Autowired in a TestExecutionListener class for BeforeClass junit

我需要在junit中执行@BeforeClass方法,但是使用Spring注入值,因此无法将private变量切换为static 我正在尝试执行此监听器并创建一个监听器类,但我遇到了这个问题。

我也需要在这个类中使用Autowire值,因为我想运行BeforeClass的方法调用@Autowired注入的变量。 但是,由于某种原因,它不起作用,此值仍为空。 有没有人遇到这样的问题?

它不是最干净的,但它有效:

public class MyTestListener extends AbstractTestExecutionListener {

    @Value("${my.value}")
    private String myValue;
    @Autowired
    private MyBean myBean;

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        testContext.getApplicationContext()
                .getAutowireCapableBeanFactory()
                .autowireBean(this);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM