Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 测试前强制重新加载弹簧_Java_Spring_Junit - Fatal编程技术网

Java 测试前强制重新加载弹簧

Java 测试前强制重新加载弹簧,java,spring,junit,Java,Spring,Junit,有没有办法在测试之前强制完全重建Spring上下文? 将其标记为@DirtiesContext不会影响当前测试。 原因:因为在测试之前,我设置了一些系统属性,这些属性会影响构建过程中的上下文。对不起,我不这么认为 我还没有尝试过这种方法,但是一种解决方法可能是在@Before或@BeforeClass方法中获取上下文,然后调用reload 您的测试应该获得上下文并将其转换为ConfigurableApplicationContext,方法是遵循中的测试上下文指导。好吧,我成功地做到了。。。但我警

有没有办法在测试之前强制完全重建Spring上下文? 将其标记为@DirtiesContext不会影响当前测试。
原因:因为在测试之前,我设置了一些系统属性,这些属性会影响构建过程中的上下文。

对不起,我不这么认为

我还没有尝试过这种方法,但是一种解决方法可能是在@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...