我的maven配置看起来像
<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>
我有一个试图设置其值的类
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;
}
}
我尝试将其测试为
public class PropertyReaderTest {
@Test
public void testGetProfile() throws Exception {
assertEquals("development", new PropertyReader().getProfile());
}
}
我认为测试失败为
Failed tests: testGetProfile(com.org.myproj.external_services.ifs.PropertyReaderTest): expected:<development> but was:<null>
我在这里失踪的是什么?
更新
我看到该属性是由Maven设置的
@Test
public void testGetProfile() throws Exception {
System.out.printf(System.getProperty("spring.active.profiles"));
assertEquals("development", new PropertyReader().getProfile());
}
我看到了
development
java.lang.AssertionError:
Expected :development
Actual :null
我甚至尝试过不同的语法
public class PropertyReader {
// @Value("#{systemProperties[spring.active.profiles]")
@Value("${spring.active.profiles")
private String profile;
@Nonnull
public String getProfile() {
return profile;
}
}
但仍然无法阅读
答案 0 :(得分:1)
简短回答
在maven-surefire-plugin配置中不推荐使用systemProperties。使用systemPropertyVariables和@Value("#{systemProperties['spring.active.profiles']}")
LONG ANSWER
我测试了它并且工作正常。我的测试详情在这里。
的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);
}
}
运行命令
> mvn test
输出:
----------------------------------------------- --------测试 -------------------------------------------------- -----运行net.yazilimsal.spring.PropertyReaderTest 2014年5月1日10:49:44 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO:刷新 org.springframework.context.support.GenericApplicationContext@2abe0e27: 启动日期[Thu May 01 22:49:44 EEST 2014];背景的根源 层次结构2014年5月1日10:49:45 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息:预先实例化单例 org.springframework.beans.factory.support.DefaultListableBeanFactory@320cf66b: 定义bean [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]; 工厂层次结构开发的根测试运行:1,失败:0, 错误:0,跳过:0,经过的时间:0.793秒 - 英寸 net.yazilimsal.spring.PropertyReaderTest
结果:
测试运行:1,失败:0,错误:0,跳过:0