forked from chanjarster/spring-test-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestPropertyTest.java
64 lines (52 loc) · 2.23 KB
/
TestPropertyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package me.chanjar.annotation.testps.ex2;
import me.chanjar.annotation.testps.ex1.PropertySourceConfig;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import java.util.Map;
import static java.util.stream.Collectors.toList;
import static org.testng.Assert.assertEquals;
@SpringBootTest(classes = PropertySourceConfig.class)
@TestPropertySource(
properties = { "foo=xyz", "bar=uvw", "PATH=aaa", "java.runtime.name=bbb" },
locations = "classpath:me/chanjar/annotation/testps/ex1/test-property-source.properties"
)
public class TestPropertyTest extends AbstractTestNGSpringContextTests implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
Map<String, Object> systemEnvironment = ((ConfigurableEnvironment) environment).getSystemEnvironment();
System.out.println("=== System Environment ===");
System.out.println(getMapString(systemEnvironment));
System.out.println();
System.out.println("=== Java System Properties ===");
Map<String, Object> systemProperties = ((ConfigurableEnvironment) environment).getSystemProperties();
System.out.println(getMapString(systemProperties));
}
@Test
public void testOverridePropertySource() {
assertEquals(environment.getProperty("foo"), "xyz");
}
@Test
public void testOverrideSystemEnvironment() {
assertEquals(environment.getProperty("PATH"), "aaa");
}
@Test
public void testOverrideJavaSystemProperties() {
assertEquals(environment.getProperty("java.runtime.name"), "bbb");
}
@Test
public void testInlineTestPropertyOverrideResourceLocationTestProperty() {
assertEquals(environment.getProperty("bar"), "uvw");
}
private String getMapString(Map<String, Object> map) {
return String.join("\n",
map.keySet().stream().map(k -> k + "=" + map.get(k)).collect(toList())
);
}
}