在Spring Boot中使用@TestPropertySource覆盖@PropertySource

Héc*_*tor 4 java spring spring-test spring-boot

我有我的Spring Boot主类:

@SpringBootApplication
@PropertySource("file:/my/file/properties")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
  //main method
}
Run Code Online (Sandbox Code Playgroud)

我正在从外部文件中读取属性(使用@PropertySource).现在,我有一个集成测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes= Application.class)
@WebIntegrationTest
@TestPropertySource("file:/my/test/file/properties") // <---
public class MyTest {
  //some tests
}
Run Code Online (Sandbox Code Playgroud)

我需要使用另一个外部属性文件,从显示在不同@PropertySourceApplication班级.出于这个原因,我已经添加了@TestPropertySource,但似乎这个注释不会覆盖@PropertySource.

我能做什么?

提前致谢.

lub*_*nac 11

用这种方式:

@TestPropertySource(locations = "classpath:test.properties")
Run Code Online (Sandbox Code Playgroud)

并将测试属性文件放入 src/test/resources