在使用 @SpringBootTest 而不是使用标准 application.properties 时,如何让 Spring 使用出于测试目的而创建的特定属性文件创建应用程序上下文?
提前致谢
最佳答案
您可以在 src/test/resources
下创建一个 application.properties
文件(或 YAML 变体)。
或者,您可以在 @TestPropertySource
中定义测试属性。注释:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(
properties = {
"foo.bar: value",
"fiz.biz: value"
})
public class FooTest {
...
}
这种方法相当于定义属性 @SpringBootTest
注释:
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
properties = {
"foo.bar: value",
"fiz.biz: value"
})
public class FooTest {
...
}
关于java - 如何使@SpringBootTest使用可配置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60456124/