Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not want to load application.yml when testing with Spring Boot

How can I tell Spring to load only application-test.yml and not application.yml file ?

I have a config class:

@ActiveProfiles("test")
@SpringBootConfiguration

public class MongoTestConfig {

    @Autowired
    MongoOperations operations;
...
}

And a test class:

@RunWith(SpringRunner.class)
@DataMongoTest
@SpringBootTest(classes = MongoTestConfig.class)
public class TagDefinitionRepositoryTest {
...
@Test
....
}

I've tried to add :

@TestPropertySource(locations = {"classpath:application-test.yml"})
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)

To my config class but it doesn't work: Spring still load application.yml

like image 349
ElArbi Avatar asked Nov 02 '25 13:11

ElArbi


1 Answers

I don't think you can tell Spring Boot to ignore application.yml completely. What you can do though is to override all the non desired properties using test specific property files.

Based on the code snippet you posted, any property in application-test.yml will override the equivalent property in application.yml.

Spring Boot considers application-test.yml specific to the profile "test" (which has a higher priority over the default application.yml). No annotation @TestPropertySource is needed.

But if you want to choose another name for your properties file, then you can use @TestProertySource, since files indicated in @TestProperySource parameters have higher priority over the others.

You might want to have a look at Spring Boot external configuration rules for resolving properties

like image 74
HL'REB Avatar answered Nov 04 '25 06:11

HL'REB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!