Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在BeforeClass junit的TestExecutionListener类中使用自动连线_Java_Spring_Junit_Listener - Fatal编程技术网

Java 在BeforeClass junit的TestExecutionListener类中使用自动连线

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

我需要在junit中执行
@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);
    }
}