spring - @Value not set via Java-configured test context -


i've got maven project uses java-configured spring (@configuration etc.). properties referenced @value stored in different places, e.g. tomcat's context.xml.

for testing i've created .properties file provide values components , services. in junit test (which uses spring test context) .properties file added via @propertysource. problem values not loaded file, instead value identifier set value, e.g. ${someflag:false} (so classcastexceptions other string). default value not set, think, values won't processed @ all.

i'm sure spring finds file because when change value of @propertysource filenotfoundexception. nevertheless i've tried different variants point file have worked (tested renaming produced filenotfoundexception):

  • classpath:/test.properties (my preferred notation)
  • /test.properties
  • file:src/test/resources/test.properties

i'm sure spring works, because when remove @value, class under test injected via @autowired in test expected.

down below you'll find problem scenario stripped down as possible. versions , dependencies please see pom.xml @ bottom.

myservice.java

package my.package.service;  // imports  @service public class myservice {      @value("${someflag:false}")     private boolean someflag;      public boolean hasflag() {         return booleanutils.istrue(someflag);     } } 

myconfiguration.java

@configuration @componentscan(basepackages = {"my.package.service"}) public class myconfiguration { } 

myservicecomponenttest.java

@runwith(springjunit4classrunner.class) @contextconfiguration(classes = {mytestconfiguration.class}) public class myservicecomponenttest {      @autowired     private myservice service;      @test     public void hasflagreturnstrue() {         assertthat(service.hasflag(), is(true));     } } 

mytestconfiguration.java

@configuration @import({myconfiguration.class}) @propertysource("classpath:/test.properties") public class mytestconfiguration { } 

src/test/resources/test.properties

someflag=true 

pom.xml

<properties>     <project.build.sourceencoding>utf-8</project.build.sourceencoding>     <spring.version>3.2.3.release</spring.version> </properties> <dependencies>     <dependency>         <groupid>org.apache.commons</groupid>         <artifactid>commons-lang3</artifactid>         <version>3.1</version>     </dependency>     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-core</artifactid>         <version>${spring.version}</version>     </dependency>     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-context</artifactid>         <version>${spring.version}</version>     </dependency>     <!-- test dependencies -->     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-test</artifactid>         <version>${spring.version}</version>         <scope>test</scope>     </dependency>     <dependency>         <groupid>org.hamcrest</groupid>         <artifactid>hamcrest-library</artifactid>         <version>1.3</version>         <scope>test</scope>     </dependency>     <dependency>         <groupid>junit</groupid>         <artifactid>junit</artifactid>         <version>4.11</version>         <scope>test</scope>     </dependency> </dependencies> 

the issue here need propertyplaceholderconfigurer responsible resolving ${..} fields, add bean creates bean:

@bean public static propertysourcesplaceholderconfigurer propertiesresolver() {     return new propertysourcesplaceholderconfigurer(); } 

Comments