Java 测试前强制重新加载弹簧
有没有办法在测试之前强制完全重建Spring上下文? 将其标记为@DirtiesContext不会影响当前测试。Java 测试前强制重新加载弹簧,java,spring,junit,Java,Spring,Junit,有没有办法在测试之前强制完全重建Spring上下文? 将其标记为@DirtiesContext不会影响当前测试。 原因:因为在测试之前,我设置了一些系统属性,这些属性会影响构建过程中的上下文。对不起,我不这么认为 我还没有尝试过这种方法,但是一种解决方法可能是在@Before或@BeforeClass方法中获取上下文,然后调用reload 您的测试应该获得上下文并将其转换为ConfigurableApplicationContext,方法是遵循中的测试上下文指导。好吧,我成功地做到了。。。但我警
原因:因为在测试之前,我设置了一些系统属性,这些属性会影响构建过程中的上下文。对不起,我不这么认为 我还没有尝试过这种方法,但是一种解决方法可能是在@Before或@BeforeClass方法中获取上下文,然后调用reload
您的测试应该获得上下文并将其转换为ConfigurableApplicationContext,方法是遵循中的测试上下文指导。好吧,我成功地做到了。。。但我警告你,这是肮脏的
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { .... })
// order of test methods is important!
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest {
static {
System.setProperty("myprop", "myvalue");
}
/**
* Fake test that forces spring to reload context before real tests.
*/
@Test
@DirtiesContext
public void aaReloadContext() {
}
/**
* Fake test that clears system property after tests.
*/
@Test
public void zzClearProperty() {
System.clearProperty("myprop");
}
... real test methods...