Peter
Peter

Reputation: 775

@Value annotation resolves in app but not in unit test

I have a Spring webapp with @Value annotations which are filled by a PropertySourcesPlaceholderConfigurer. The annotations resolve correctly in my webapp.

However, when I run a unit test with SpringJunit4ClassRunner and a specific bean profile for unit tests, I get an "unable to resolve placeholder xxx in string ${xxx}" exception, no matter if I run the test from Eclipse or from Maven.

Interestingly though, the PropertySourcesPlcHlCfg. gets initialized (correctly and only once) and finds the properties file, as if I rename it, I get a FileNotFoundException.

What could be going wrong here?

Thanks!

Upvotes: 0

Views: 112

Answers (1)

shankarsh15
shankarsh15

Reputation: 1967

You can try using (in case of Spring 4)

@TestPropertySource(value="classpath:/test.properties")

for loading a specific file for Junit Test.

Alternatively you can also try creating a bean PropertySourcesPlaceholderConfigurer in your configuration

@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
    return new PropertySourcesPlaceholderConfigurer();
}

Upvotes: 1

Related Questions