Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/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 每次测试后重新加载Spring应用程序上下文_Java_Spring_Junit - Fatal编程技术网

Java 每次测试后重新加载Spring应用程序上下文

Java 每次测试后重新加载Spring应用程序上下文,java,spring,junit,Java,Spring,Junit,我有一个测试类,它包含两个测试: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContextTest.xml" }) @Transactional @TransactionConfiguration(defaultRollback = true) public class MyITest extends implements BeanFacto

我有一个测试类,它包含两个测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)

public class MyITest extends implements BeanFactoryAware {

    private BeanFactory beanFactory;

    @Test
    public void test1() throws Exception {}

    @Test
    public void test2() throws Exception {}        
}
当我单独运行测试时,不会出现错误,但当我同时运行所有测试时,会出现故障。此失败是由于某些测试修改了应用程序上下文:

  b = beanFactory.getBean("logDataSource", BasicDataSource.class);
  b.set ...

是否有单独运行此测试的选项?我只想在test1启动时读取所有必要的内容,然后运行测试,然后关闭所有必要的内容。然后启动test2。

您可以在修改应用程序上下文的测试类上使用@DirtiesContext注释

默认情况下,这将在运行整个测试类后将应用程序上下文标记为脏。如果您想在单个测试方法之后将上下文标记为脏的,那么您可以在类级别的注释中将测试方法改为注释,或者在类级别的注释中将classMode属性设置为after_EACH_test_method

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)

尼斯的作品像一个魅力thx很多。只有一个限制。当我使用annotation@Transactional时,仍然有一些不好的地方:)很高兴它成功了。您可能希望在导致问题的测试方法上尝试@Rollback注释。最好将测试分成两个类别,这样就不会导致另一个测试失败。