简体   繁体   中英

how to load a different application.properties file in SpringBootTest

I am using the following annotations in my spring boot testing files

@RunWith(SpringRunner.class) // tells JUnit to run using Spring’s testing support.
@SpringBootTest // is saying “bootstrap with Spring Boot’s support” (e.g. load application.properties and give me all the Spring Boot goodness)
@AutoConfigureMockMvc

When I test rest controllers, it will show java.lang.IllegalStateException: Failed to load ApplicationContext .

I checked the log, and this is caused by connection issues with JMS and database. For my rest controllers testing, I do not need any of them. But their configuration is defined in the application.properties file and it will be loaded in the SpringBootTest, how can I use a different application.properties file for the testing purpose?

we can use @TestPropertySource or @PropertySource to load the properties file.

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:properties.yml")
@ActiveProfiles("test")
public class CartLoadApplicationTests {
    @Test
    public void contextLoads() {
    }    
}

This works after I put application.properties file in the test directory

src/test/resources/application.properties

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