在TestExecutionListener类中为BeforeClass junit使用Autowired
我需要在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); } }