Han*_*örr 84 java spring environment-variables spring-test
我想编写一些检查已部署WAR的XML Spring配置的测试.不幸的是,某些bean需要设置一些环境变量或系统属性.在使用带有@ContextConfiguration的方便测试样式时,如何在初始化spring bean之前设置环境变量?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext { ... }
Run Code Online (Sandbox Code Playgroud)
如果我使用注释配置应用程序上下文,我没有看到一个钩子,我可以在初始化spring上下文之前执行某些操作.
Jim*_*aet 110
您可以在静态初始值设定项中初始化System属性:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext {
static {
System.setProperty("myproperty", "foo");
}
}
Run Code Online (Sandbox Code Playgroud)
静态初始化程序代码将在初始化spring应用程序上下文之前执行.
Ram*_*man 79
从Spring 4.1开始,正确的方法是使用@TestPropertySource
注释.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
@TestPropertySource(properties = {"myproperty = foo"})
public class TestWarSpringContext {
...
}
Run Code Online (Sandbox Code Playgroud)
请参阅Spring docs和Javadocs中的@TestPropertySource .
Syl*_*are 10
对于 springboot,我认为这是最简单的方法,使用java@SpringBootTest
中的注释:
@SpringBootTest(
properties = { "spring.application.name=example", "ENV_VARIABLE=secret" }
)
public class ApplicationTest {
// Write your tests here
}
Run Code Online (Sandbox Code Playgroud)
或者在kotlin中你可以这样做:
@SpringBootTest(
properties = ["spring.application.name=example", "ENV_VARIABLE=secret"]
)
internal class ApplicationKTest {
// Write your tests here
}
Run Code Online (Sandbox Code Playgroud)
这就是您的测试应该运行的,并使用您在注释中定义的属性覆盖属性。假设你的样子是application.yml
这样的:
spring:
application:
name: "app"
db:
username: "user"
password: ${ENV_VARIABLE:default}
Run Code Online (Sandbox Code Playgroud)
那么在测试过程中它将是:
spring.application.name
将返回值"example"
ENV_VARIABLE
将返回"secret"
,因此如果您在代码中使用该值,db.password
它将返回"secret"
。也可以使用测试ApplicationContextInitializer来初始化系统属性:
public class TestApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
@Override
public void initialize(ConfigurableApplicationContext applicationContext)
{
System.setProperty("myproperty", "value");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Spring上下文配置文件位置之外在测试类上配置它:
@ContextConfiguration(initializers = TestApplicationContextInitializer.class, locations = "classpath:whereever/context.xml", ...)
@RunWith(SpringJUnit4ClassRunner.class)
public class SomeTest
{
...
}
Run Code Online (Sandbox Code Playgroud)
这样,如果应为所有单元测试设置某个系统属性,则可以避免代码重复.
如果你希望你的变量对所有测试都有效,你可以application.properties
在你的测试资源目录(默认情况下:)中有一个文件,src/test/resources
它看起来像这样:
MYPROPERTY=foo
Run Code Online (Sandbox Code Playgroud)
这将被加载和使用,除非您通过@TestPropertySource
或类似的方法定义- 加载属性的确切顺序可以在 Spring 文档第24章外部化配置中找到。
目前,这里所有的答案都只讨论系统属性,这些属性与设置更复杂的环境变量(特别是环境变量)不同。用于测试。值得庆幸的是,下面的类可以用于此,并且类文档中有很好的示例
来自文档的快速示例,已修改为与@SpringBootTest一起使用
@SpringBootTest
public class EnvironmentVariablesTest {
@ClassRule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables().set("name", "value");
@Test
public void test() {
assertEquals("value", System.getenv("name"));
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
116207 次 |
最近记录: |