Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override @PropertySource with @TestPropertySource in Spring Boot

I have my Spring Boot main class:

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
  //main method
}

I'm reading properties from an external file (using @PropertySource). Now, I have an integration test:

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

I need to use another external properties file, different from the indicated in @PropertySource in Application class. For that reason, I have added @TestPropertySource, but seems that this annotation is not overriding the @PropertySource.

What can I do?

Thanks in advance.

like image 323
Héctor Avatar asked Apr 14 '16 06:04

Héctor


1 Answers

Use it this way:

@TestPropertySource(locations = "classpath:test.properties")

and place test properties file into src/test/resources

like image 121
luboskrnac Avatar answered Sep 22 '22 11:09

luboskrnac