【发布时间】:2014-10-23 05:50:45
【问题描述】:
我有一个代码要测试,它取决于环境变量集的值。 如何模拟系统环境变量的设置? 我不想在我的测试类中执行 System.setProperty(),这可能会将变量添加到环境中。
我只想模拟设置和测试。如何模拟它??
谢谢。
【问题讨论】:
标签: spring unit-testing integration-testing spring-batch spring-boot
我有一个代码要测试,它取决于环境变量集的值。 如何模拟系统环境变量的设置? 我不想在我的测试类中执行 System.setProperty(),这可能会将变量添加到环境中。
我只想模拟设置和测试。如何模拟它??
谢谢。
【问题讨论】:
标签: spring unit-testing integration-testing spring-batch spring-boot
如果您的代码确实依赖于系统属性,我建议将其重构为依赖于 Spring Environment。然后您可以在您的 Spring 测试中使用(无论如何使用 Spring Boot)@IntegrationTest,例如(来自the docs):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@IntegrationTest({"foo=bar"})
public class SomeIntegrationTests {
// ...
}
【讨论】:
@ActiveProfile。