Java 在BeforeClass junit的TestExecutionListener类中使用自动连线
我需要在junit中执行Java 在BeforeClass junit的TestExecutionListener类中使用自动连线,java,spring,junit,listener,Java,Spring,Junit,Listener,我需要在junit中执行@BeforeClass方法,但使用Spring注入值,因此无法将private变量切换到static。我正在尝试使用Listeners并创建一个Listener类,但是我遇到了一个问题 在这个类中,我有一些值需要Autowired,因为我想要在类之前运行Autowired的方法调用的变量是@Autowired注入的。但是,由于某些原因,它不起作用,并且该值仍然为空。以前有人遇到过这样的问题吗?这不是最干净的,但它可以工作: public class MyTestList
@BeforeClass
方法,但使用Spring注入值,因此无法将private
变量切换到static
。我正在尝试使用Listeners并创建一个Listener类,但是我遇到了一个问题
在这个类中,我有一些值需要
Autowired
,因为我想要在类之前运行Autowired
的方法调用的变量是@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);
}
}