daydreamer
daydreamer

Reputation: 91959

Spring: Can not read system property value set by maven

My maven config looks like

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <systemProperties>
                            <spring.active.profiles>development</spring.active.profiles>
                        </systemProperties>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

and I have a class which tries to set its value

import javax.annotation.Nonnull;

import org.springframework.beans.factory.annotation.Value;

public class PropertyReader {
    @Value("${spring.active.profiles}")
    private String profile;

    @Nonnull
    public String getProfile() {
        return profile;
    }
}

I try to test it as

public class PropertyReaderTest {

        @Test
        public void testGetProfile() throws Exception {
            assertEquals("development", new PropertyReader().getProfile());
        }
    }

I see test failure as

Failed tests:   testGetProfile(com.org.myproj.external_services.ifs.PropertyReaderTest): expected:<development> but was:<null>

What is that I am missing here?

UPDATE

I see that the property is set by Maven

@Test
public void testGetProfile() throws Exception {
    System.out.printf(System.getProperty("spring.active.profiles"));
    assertEquals("development", new PropertyReader().getProfile());
}

I see

development
java.lang.AssertionError: 
Expected :development
Actual   :null

I even tried different syntaxes

public class PropertyReader {
//    @Value("#{systemProperties[spring.active.profiles]")
    @Value("${spring.active.profiles")
    private String profile;

    @Nonnull
    public String getProfile() {
        return profile;
    }
}

But still can't read it

Upvotes: 1

Views: 3060

Answers (1)

bhdrk
bhdrk

Reputation: 3495

SHORT ANSWER

systemProperties is deprecated in maven-surefire-plugin configuration. use systemPropertyVariables and @Value("#{systemProperties['spring.active.profiles']}")

LONG ANSWER

I tested it and worked fine. My test details here.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.yazilimsal</groupId>
    <artifactId>sampleapp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
               <configuration>
                   <systemPropertyVariables>
                       <spring.active.profiles>development</spring.active.profiles>
                   </systemPropertyVariables>
               </configuration>
            </plugin>
        </plugins>
    </build>

</project>

PropertyReader.java

import org.springframework.beans.factory.annotation.Value;

public class PropertyReader {

    @Value("#{systemProperties['spring.active.profiles']}")
    private String profiles;

    public String getProfiles() {
        return profiles;
    }

    public void setProfiles(String profiles) {
        this.profiles = profiles;
    }
}

AppConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class AppConfig {

    @Bean
    public PropertyReader propertyReader() {
        return new PropertyReader();
    }

}

PropertyReaderTest.java

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class PropertyReaderTest {

    @Autowired
    private PropertyReader propertyReader;

    @Test
    public void testSpringActiveProfiles() throws Exception {
        String profiles = propertyReader.getProfiles();

        System.out.println(profiles);
        Assert.assertEquals("development", profiles);
    }
}

Run command

> mvn test 

Output:

------------------------------------------------------- T E S T S ------------------------------------------------------- Running net.yazilimsal.spring.PropertyReaderTest May 1, 2014 10:49:44 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.GenericApplicationContext@2abe0e27: startup date [Thu May 01 22:49:44 EEST 2014]; root of context hierarchy May 1, 2014 10:49:45 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@320cf66b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,appConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,propertyReader]; root of factory hierarchy development Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.793 sec - in net.yazilimsal.spring.PropertyReaderTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Upvotes: 3

Related Questions