简体   繁体   中英

How to make @SpringBootTest use configurable properties

How can I make Spring create an application context using a specific properties file made for testing purposes when using @SpringBootTest rather than using the standard application.properties?

Thanks in advance

You could create an application.properties file (or YAML variant) under src/test/resources .


Alternatively, you can define properties for test in the @TestPropertySource annotation:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(
        properties = {
                "foo.bar: value",
                "fiz.biz: value"
        })
public class FooTest {
    ...
}

This approach is equivalent to defining the properties the @SpringBootTest annotation:

@RunWith(SpringRunner.class)
@SpringBootTest(
        webEnvironment = WebEnvironment.RANDOM_PORT,
        properties = {
                "foo.bar: value",
                "fiz.biz: value"
        })
public class FooTest {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM