我试图弄清楚如何使用 Spring Profiles 进行测试。我想我会根据 spring 文档做所有事情。最后我得到了我无法解释的结果这是我的程序清单:
这是主要配置:
package com.test.profiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.util.HashMap;
import java.util.Map;
@Configuration
@ComponentScan("com.test.profiles")
public class MainConfig {
@Autowired
private String helloMsg;
@Autowired
private Map<String,String> profileDependentProps;
@Bean
@Profile("default")
public String helloMsg() {
return "Hello default";
}
@Bean
@Profile("default")
public Map<String,String> profileDependentProps() {
Map<String,String> props = new HashMap<>();
props.put("1", "default");
props.put("2", "default");
props.put("3", "default");
return props;
}
}
测试配置:
package com.test.profiles;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.util.HashMap;
import java.util.Map;
@Configuration
@Profile("dev")
public class TestConfig {
@Bean
public String helloMsg() {
return "Hello dev";
}
@Bean
public Map<String,String> profileDependentProps() {
Map<String,String> props = new HashMap<>();
props.put("1", "dev");
props.put("2", "dev");
props.put("3", "dev");
return props;
}
}
最后是我的测试课:
package com.test.profiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import java.util.Map;
@ContextConfiguration(classes = MainConfig.class)
@ActiveProfiles("dev")
public class ProfileTester extends AbstractTestNGSpringContextTests {
@Autowired
private String string;
@Autowired
private Map<String,String> profileDependentProps;
@Test
public void profileTest() {
System.out.println("TEST: "+string);
for(Map.Entry<String,String> entry : profileDependentProps.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
}
奇数输出:
[TestNG] Running:
C:\Users\nikopol\.IntelliJIdea13\system\temp-testng-customsuite.xml
15:36:34,893 INFO GenericApplicationContext:513 - Refreshing org.springframework.context.support.GenericApplicationContext@7ecdc69b: startup date [Mon Mar 10 15:36:34 EET 2014]; root of context hierarchy
TEST: Hello dev
helloMsg=Hello dev
===============================================
Custom suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
15:36:35,105 INFO GenericApplicationContext:873 - Closing org.springframework.context.support.GenericApplicationContext@7ecdc69b: startup date [Mon Mar 10 15:36:34 EET 2014]; root of context hierarchy
这条线是怎么回事?--- helloMsg=Hello dev 我的地图在哪里?