我想编写一些测试来检查已部署 WAR 的 XML Spring 配置。不幸的是,有些 bean 需要设置一些环境变量或系统属性。使用@ContextConfiguration方便的测试样式时,如何在spring bean初始化之前设置环境变量?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext { ... }
如果我使用注释配置应用程序上下文,我看不到在 spring 上下文初始化之前我可以做某事的钩子(Hook)。
最佳答案
您可以在静态初始化程序中初始化 System 属性:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext {
static {
System.setProperty("myproperty", "foo");
}
}
静态初始化代码将在spring应用上下文初始化之前执行。
关于java - 如何在 Spring 测试中设置环境变量或系统属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306951/