Skip to content

Commit 4a1dbba

Browse files
committed
spring @value default value
1 parent 71d1fb5 commit 4a1dbba

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.ripjava.spring.properties.valuewithdefaults;
2+
3+
4+
import com.ripjava.spring.properties.ConfigPropertiesDemoApplication;
5+
import com.ripjava.spring.properties.yaml.YamlConfigApplication;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.TestPropertySource;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
12+
import org.springframework.util.Assert;
13+
14+
import java.util.Arrays;
15+
import java.util.List;
16+
import java.util.function.IntConsumer;
17+
import java.util.stream.Collectors;
18+
import java.util.stream.IntStream;
19+
import java.util.stream.Stream;
20+
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
@ExtendWith(SpringExtension.class)
26+
@TestPropertySource(value = "classpath:valueswithdefaults.properties")
27+
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
28+
public class ValuesWithDefaultsAppTest {
29+
@Value("${some.key:my default value}")
30+
private String stringWithDefaultValue;
31+
32+
@Value("${some.key:}")
33+
private String stringWithBlankDefaultValue;
34+
35+
@Value("${some.key:true}")
36+
private boolean booleanWithDefaultValue;
37+
38+
@Value("${some.key:42}")
39+
private int intWithDefaultValue;
40+
41+
@Value("${some.key:one,two,three}")
42+
private String[] stringArrayWithDefaults;
43+
44+
@Value("${some.key:1,2,3}")
45+
private int[] intArrayWithDefaults;
46+
47+
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
48+
private String spelWithDefaultValue;
49+
50+
51+
@Test
52+
public void test_ValuesWithDefaults() {
53+
54+
assertEquals(stringWithDefaultValue, "my default value");
55+
assertEquals(stringWithBlankDefaultValue, "");
56+
57+
58+
assertTrue(booleanWithDefaultValue);
59+
assertTrue(intWithDefaultValue == 42);
60+
61+
List<String> stringListValues = Arrays.asList("one", "two", "three");
62+
assertArrayEquals(stringArrayWithDefaults, stringListValues.toArray());
63+
64+
List<Integer> intListValues = Arrays.asList(1, 2, 3);
65+
assertEquals(IntStream.of(intArrayWithDefaults).boxed().collect(Collectors.toList()), intListValues);
66+
67+
assertEquals(spelWithDefaultValue, "my default system property value");
68+
}
69+
70+
}

spring-properties/src/test/ressources/valueswithdefaults.properties

Whitespace-only changes.

0 commit comments

Comments
 (0)