Is there a way to get list of loaded properties file in SpringBoot application?

Viewed 4284

I'm facing an issue on only one platform when I'm trying to execute mvn clean install. As part of the build we compile multiple component and last we execute functional testing using wiremock. It is supposed to pick specific configuration from function testing profile and default properties should be picked from application.properties file. But for some reason same code isn't able to find the properties mentioned in these file. So, just wondering if somehow, if I can get the list of properties files being loaded during wiremock ? This will give some clue on why isn't expected properties files are being picked ?

All properties files are located inside :

src/main/resources

And, following from test class.

@ContextConfiguration(classes = SampleFTConfiguration.class)
public class SampleControllerTest{
//test method
}

@ComponentScan("com.xxx.xxx.xxx.ft")
@PropertySource("classpath:application-test.properties")
public class  SampleFTConfiguration{


}

Note : I'm not expecting anyone to fix the issue, all I wanted to know, if we can get the name of loaded property files ?

2 Answers

After searching and trying out for a while, looks like ConfigurableEnvironment is what you're trying to find.

The code is pretty simple. However I think it's better to debug and check the configurableEnvironment value directly, so you can adjust the code to your needs (remove filter name, etc).

  @Autowired
  private ConfigurableEnvironment configurableEnvironment;

  @Test
  public void getProperties() {
    Map<String, Object> mapOfProperties = configurableEnvironment.getPropertySources()
        .stream()
        .filter(propertySource -> propertySource.getName()
            .contains("application-test.properties"))
        .collect(Collectors.toMap(PropertySource::getName, PropertySource::getSource));
    mapOfProperties.values()
        .forEach(System.out::println);
  }

the code will printed out

{properties-one=value-for-properties-one, properties-two=value-for-properties-two}

with my application-test.properties value

properties-one=value-for-properties-one
properties-two=value-for-properties-two

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/ConfigurableEnvironment.html

Ok, following the test definition please make sure that:

  1. You should run the test with spring runner (spring extension if you're on JUnit5). So you should place the annotation @RunWith(SpringRunner.class) (or @ExtendsWith(SpringExtension.class) for junit 5)

  2. The property source you're using is application-test.properties. You've said that the properties file is located in src/main/resources but the file name probably implies that it should reside in src/test/resources

Related