在使用@SpringBootTest而不是使用标准application.properties时,如何使Spring使用为测试目的而创建的特定属性文件创建应用程序上下文?
预先感谢
答案 0 :(得分:1)
您可以在application.properties
下创建一个src/test/resources
文件(或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 {
...
}