├── .circleci
└── config.yml
├── README.md
├── k8s
├── configmap.yaml
└── deployment.yaml
├── pom.xml
├── renovate.json
├── skaffold.yaml
└── src
├── main
├── java
│ └── pl
│ │ └── piomin
│ │ └── services
│ │ └── playground
│ │ ├── ConfigApp.java
│ │ ├── beans
│ │ ├── MyBean1.java
│ │ ├── MyBean2.java
│ │ ├── MyBean3.java
│ │ ├── MyBean4.java
│ │ ├── MyBean5.java
│ │ ├── MyBean6.java
│ │ ├── MyBeansOrPropertyCondition.java
│ │ ├── MyConfiguration.java
│ │ └── MyConfigurationOverride.java
│ │ └── model
│ │ ├── Person.java
│ │ └── PersonsList.java
└── resources
│ ├── additional-test.yml
│ ├── additional.yml
│ ├── app.yml
│ ├── application-override.yml
│ ├── application.yml
│ ├── global.yml
│ ├── override.yml
│ └── sample-appconfig.yml
└── test
└── java
└── pl
└── piomin
└── services
└── playground
├── AutoConfigurationTest.java
├── ConfigurationPropertiesTest.java
├── LoadingOrderFourthTest.java
├── LoadingOrderSecondTest.java
├── LoadingOrderTest.java
└── LoadingOrderThirdTest.java
/.circleci/config.yml:
--------------------------------------------------------------------------------
1 | version: 2.1
2 |
3 | jobs:
4 | build:
5 | docker:
6 | - image: 'cimg/openjdk:21.0.6'
7 | steps:
8 | - checkout
9 | - run:
10 | name: Analyze on SonarCloud
11 | command: mvn verify sonar:sonar
12 |
13 | executors:
14 | jdk:
15 | docker:
16 | - image: 'cimg/openjdk:21.0.6'
17 |
18 | orbs:
19 | maven: circleci/maven@2.0.0
20 |
21 | workflows:
22 | maven_test:
23 | jobs:
24 | - maven/test:
25 | executor: jdk
26 | - build:
27 | context: SonarCloud
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Demo Project Showing Spring Boot Configuration [](https://twitter.com/piotr_minkowski)
2 |
3 | [](https://circleci.com/gh/piomin/springboot-configuration-playground)
4 |
5 | [](https://sonarcloud.io/dashboard?id=piomin_springboot-configuration-playground)
6 | [](https://sonarcloud.io/dashboard?id=piomin_springboot-configuration-playground)
7 | [](https://sonarcloud.io/dashboard?id=piomin_springboot-configuration-playground)
8 | [](https://sonarcloud.io/dashboard?id=piomin_springboot-configuration-playground)
9 |
10 | This repository has been used as the example for several articles. Detailed description can be found in the following posts:
11 | 1. How to set Spring Boot configuration when running app on Kubernetes. Post: [A Deep Dive Into Spring Boot Configuration](https://piotrminkowski.com/2022/08/02/a-deep-dive-into-spring-boot-configuration/)
12 | 2. How to set Spring Boot auto-configuration. Post: [A Magic Around Spring Boot Auto Configuration](https://piotrminkowski.com/2020/01/21/a-magic-around-spring-boot-auto-configuration/)
13 | 3. How to set Spring Boot externalized configuration. Post: [A Magic Around Spring Boot Externalized Configuration](https://piotrminkowski.com/2019/03/11/a-magic-around-spring-boot-externalized-configuration/)
--------------------------------------------------------------------------------
/k8s/configmap.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: ConfigMap
3 | metadata:
4 | name: springboot-configuration-playground
5 | data:
6 | PROPERTY_DEFAULT: env
7 | SPRING_APPLICATION_JSON: '{"property":{"default":"json"}}'
8 | ---
9 | apiVersion: v1
10 | kind: ConfigMap
11 | metadata:
12 | name: springboot-configuration-playground-ext
13 | data:
14 | application.yml: |
15 | property.default: external
--------------------------------------------------------------------------------
/k8s/deployment.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: apps/v1
2 | kind: Deployment
3 | metadata:
4 | name: springboot-configuration-playground
5 | spec:
6 | selector:
7 | matchLabels:
8 | app: springboot-configuration-playground
9 | template:
10 | metadata:
11 | labels:
12 | app: springboot-configuration-playground
13 | spec:
14 | containers:
15 | - name: springboot-configuration-playground
16 | image: piomin/springboot-configuration-playground
17 | ports:
18 | - containerPort: 8080
19 | volumeMounts:
20 | - mountPath: /config
21 | name: app-vol
22 | envFrom:
23 | - configMapRef:
24 | name: springboot-configuration-playground
25 | volumes:
26 | - name: app-vol
27 | configMap:
28 | name: springboot-configuration-playground-ext
29 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | org.springframework.boot
9 | spring-boot-starter-parent
10 | 3.5.0
11 |
12 |
13 | pl.piomin.services
14 | springboot-configuration-playground
15 | 1.1-SNAPSHOT
16 |
17 |
18 | 21
19 | piomin_springboot-configuration-playground
20 | piomin
21 | https://sonarcloud.io
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-test
32 | test
33 |
34 |
35 |
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-maven-plugin
41 |
42 |
43 | com.google.cloud.tools
44 | jib-maven-plugin
45 | 3.4.5
46 |
47 |
48 | org.jacoco
49 | jacoco-maven-plugin
50 | 0.8.13
51 |
52 |
53 |
54 | prepare-agent
55 |
56 |
57 |
58 | report
59 | test
60 |
61 | report
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/renovate.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3 | "extends": [
4 | "config:base",":dependencyDashboard"
5 | ],
6 | "packageRules": [
7 | {
8 | "matchUpdateTypes": ["minor", "patch", "pin", "digest"],
9 | "automerge": true
10 | }
11 | ],
12 | "prCreation": "not-pending"
13 | }
--------------------------------------------------------------------------------
/skaffold.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: skaffold/v1
2 | kind: Config
3 | build:
4 | artifacts:
5 | - image: piomin/springboot-configuration-playground
6 | jib:
7 | args:
8 | - -DskipTests
9 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/ConfigApp.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import jakarta.annotation.PostConstruct;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.beans.factory.annotation.Value;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.context.annotation.PropertySource;
10 |
11 | import java.util.Map;
12 |
13 | @SpringBootApplication
14 | @PropertySource(value = "classpath:/additional.yml", ignoreResourceNotFound = true)
15 | public class ConfigApp {
16 |
17 | private static final Logger LOG = LoggerFactory.getLogger(ConfigApp.class);
18 |
19 | public static void main(String[] args) {
20 | SpringApplication app = new SpringApplication(ConfigApp.class);
21 | app.setDefaultProperties(Map.of("property.default", "default"));
22 | app.setAllowBeanDefinitionOverriding(true);
23 | app.run(args);
24 | }
25 |
26 | @Value("${property.default}")
27 | String propertyDefault;
28 | @Value("${property.activation:none}")
29 | String propertyActivation;
30 |
31 | @PostConstruct
32 | public void printInfo() {
33 | LOG.info("========= Property (default): {}", propertyDefault);
34 | LOG.info("========= Property (activation): {}", propertyActivation);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBean1.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | public class MyBean1 {
4 |
5 | public String me() {
6 | return "I'm MyBean1";
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBean2.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | public class MyBean2 {
4 |
5 | private String me = "I'm MyBean2";
6 |
7 | public String me() {
8 | return me;
9 | }
10 |
11 | void setMe(String me) {
12 | this.me = me;
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBean3.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | public class MyBean3 {
4 |
5 | public String me() {
6 | return "I'm MyBean3";
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBean4.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | public class MyBean4 {
4 |
5 | public String me() {
6 | return "I'm MyBean4";
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBean5.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | public class MyBean5 {
4 |
5 | public String me() {
6 | return "I'm MyBean5";
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBean6.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | public class MyBean6 {
4 |
5 | public String me() {
6 | return "I'm MyBean6";
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyBeansOrPropertyCondition.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
6 |
7 | public class MyBeansOrPropertyCondition extends AnyNestedCondition {
8 |
9 | public MyBeansOrPropertyCondition() {
10 | super(ConfigurationPhase.REGISTER_BEAN);
11 | }
12 |
13 | @ConditionalOnBean(MyBean1.class)
14 | static class MyBean1ExistsCondition {
15 |
16 | }
17 |
18 | @ConditionalOnBean(MyBean2.class)
19 | static class MyBean2ExistsCondition {
20 |
21 | }
22 |
23 | @ConditionalOnProperty("multipleBeans.enabled")
24 | static class MultipleBeansPropertyExists {
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyConfiguration.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
7 | import org.springframework.boot.system.JavaVersion;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Conditional;
10 |
11 | public class MyConfiguration {
12 |
13 | @Bean
14 | @ConditionalOnClass(MyBean2.class)
15 | public MyBean1 myBean1() {
16 | return new MyBean1();
17 | }
18 |
19 | @Bean
20 | @ConditionalOnProperty("myBean2.enabled")
21 | public MyBean2 myBean2() {
22 | return new MyBean2();
23 | }
24 |
25 | @Bean
26 | @ConditionalOnJava(range = ConditionalOnJava.Range.OLDER_THAN, value = JavaVersion.EIGHTEEN)
27 | public MyBean3 myBean3() {
28 | return new MyBean3();
29 | }
30 |
31 | @Bean
32 | @ConditionalOnProperty("multipleBeans.enabled")
33 | @ConditionalOnBean({MyBean1.class, MyBean2.class})
34 | public MyBean4 myBean4() {
35 | return new MyBean4();
36 | }
37 |
38 | @Bean
39 | @ConditionalOnProperty(value = "myBean5.disabled", havingValue = "false")
40 | public MyBean5 myBean5() {
41 | return new MyBean5();
42 | }
43 |
44 | @Bean
45 | @Conditional(MyBeansOrPropertyCondition.class)
46 | public MyBean6 myBean6() {
47 | return new MyBean6();
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/beans/MyConfigurationOverride.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.beans;
2 |
3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4 | import org.springframework.context.annotation.Bean;
5 |
6 | public class MyConfigurationOverride {
7 |
8 | @Bean
9 | public MyBean2 myBean2() {
10 | MyBean2 b = new MyBean2();
11 | b.setMe("I'm MyBean2 overridden");
12 | return b;
13 | }
14 |
15 | @Bean
16 | @ConditionalOnProperty("multiple.beans")
17 | public MyBean2 myBeanX() {
18 | MyBean2 b = new MyBean2();
19 | b.setMe("I'm myBean2 overridden");
20 | return b;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/model/Person.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.model;
2 |
3 | public class Person {
4 |
5 | private String firstName;
6 | private String lastName;
7 | private int age;
8 |
9 | public String getFirstName() {
10 | return firstName;
11 | }
12 |
13 | public void setFirstName(String firstName) {
14 | this.firstName = firstName;
15 | }
16 |
17 | public String getLastName() {
18 | return lastName;
19 | }
20 |
21 | public void setLastName(String lastName) {
22 | this.lastName = lastName;
23 | }
24 |
25 | public int getAge() {
26 | return age;
27 | }
28 |
29 | public void setAge(int age) {
30 | this.age = age;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/pl/piomin/services/playground/model/PersonsList.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground.model;
2 |
3 | import org.springframework.boot.context.properties.ConfigurationProperties;
4 | import org.springframework.stereotype.Component;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | @Component
10 | @ConfigurationProperties("persons-list")
11 | public class PersonsList {
12 |
13 | private List persons = new ArrayList<>();
14 |
15 | public List getPersons() {
16 | return persons;
17 | }
18 |
19 | public void setPersons(List persons) {
20 | this.persons = persons;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/resources/additional-test.yml:
--------------------------------------------------------------------------------
1 | property2: additional-test
2 | property3: additional-test
--------------------------------------------------------------------------------
/src/main/resources/additional.yml:
--------------------------------------------------------------------------------
1 | property1: additional
2 | property2: additional
3 | property3: additional
4 | property4: additional
5 |
6 | property.default: additional
--------------------------------------------------------------------------------
/src/main/resources/app.yml:
--------------------------------------------------------------------------------
1 | property3: app
--------------------------------------------------------------------------------
/src/main/resources/application-override.yml:
--------------------------------------------------------------------------------
1 | property2: override
2 | property3: override
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | property1: app
2 | property2: app
3 |
4 | property.default: app
5 |
6 | persons-list.persons:
7 | - firstName: John
8 | lastName: Smith
9 | age: 30
10 | - firstName: Tom
11 | lastName: Walker
12 | age: 40
13 | - firstName: Kate
14 | lastName: Hamilton
15 | age: 50
16 | ---
17 | spring:
18 | config:
19 | activate:
20 | on-cloud-platform: "kubernetes"
21 | property.activation: "I'm on Kubernetes!"
--------------------------------------------------------------------------------
/src/main/resources/global.yml:
--------------------------------------------------------------------------------
1 | property1: global
2 | property2: global
3 | property3: global
--------------------------------------------------------------------------------
/src/main/resources/override.yml:
--------------------------------------------------------------------------------
1 | property2: override
2 | property3: override
--------------------------------------------------------------------------------
/src/main/resources/sample-appconfig.yml:
--------------------------------------------------------------------------------
1 | property2: sample
2 | property3: sample
--------------------------------------------------------------------------------
/src/test/java/pl/piomin/services/playground/AutoConfigurationTest.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
8 | import org.springframework.boot.test.context.FilteredClassLoader;
9 | import org.springframework.boot.test.context.SpringBootTest;
10 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
11 | import pl.piomin.services.playground.beans.*;
12 |
13 | @SpringBootTest
14 | public class AutoConfigurationTest {
15 |
16 | private static final Logger LOGGER = LoggerFactory.getLogger(AutoConfigurationTest.class);
17 |
18 | @Test
19 | public void testMyBean1() {
20 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
21 | Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
22 | contextRunner
23 | .withUserConfiguration(MyConfiguration.class)
24 | .withClassLoader(new FilteredClassLoader(MyBean2.class))
25 | .run(context -> {
26 | MyBean1 myBean1 = context.getBean(MyBean1.class);
27 | Assertions.assertEquals("I'm MyBean1", myBean1.me());
28 | });
29 | });
30 | }
31 |
32 | @Test
33 | public void testMyBean2Negative() {
34 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
35 | Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
36 | contextRunner
37 | .withPropertyValues("myBean2.enabled=false")
38 | .withUserConfiguration(MyConfiguration.class)
39 | .run(context -> {
40 | MyBean2 myBean2 = context.getBean(MyBean2.class);
41 | Assertions.assertEquals("I'm MyBean2", myBean2.me());
42 | });
43 | });
44 | }
45 |
46 | @Test
47 | public void testMyBean2Positive() {
48 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
49 | contextRunner
50 | .withPropertyValues("myBean2.enabled=true")
51 | .withUserConfiguration(MyConfiguration.class)
52 | .run(context -> {
53 | MyBean2 myBean2 = context.getBean(MyBean2.class);
54 | Assertions.assertEquals("I'm MyBean2", myBean2.me());
55 | });
56 | }
57 |
58 | @Test
59 | public void testMyBean3() {
60 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
61 | Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
62 | contextRunner
63 | .withUserConfiguration(MyConfiguration.class)
64 | .run(context -> {
65 | MyBean3 myBean3 = context.getBean(MyBean3.class);
66 | Assertions.assertEquals("I'm MyBean3", myBean3.me());
67 | });
68 | });
69 | }
70 |
71 | @Test
72 | public void testMyBean4Negative() {
73 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
74 | Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
75 | contextRunner
76 | .withUserConfiguration(MyConfiguration.class)
77 | .withPropertyValues("multipleBeans.enabled")
78 | .run(context -> {
79 | MyBean4 myBean4 = context.getBean(MyBean4.class);
80 | Assertions.assertEquals("I'm MyBean4", myBean4.me());
81 | });
82 | });
83 | }
84 |
85 | @Test
86 | public void testMyBean4Positive() {
87 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
88 | contextRunner
89 | .withUserConfiguration(MyConfiguration.class)
90 | .withPropertyValues("multipleBeans.enabled", "myBean2.enabled")
91 | .run(context -> {
92 | MyBean4 myBean4 = context.getBean(MyBean4.class);
93 | Assertions.assertEquals("I'm MyBean4", myBean4.me());
94 | });
95 | }
96 |
97 | @Test
98 | public void testOrder() {
99 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
100 | contextRunner
101 | .withAllowBeanDefinitionOverriding(true)
102 | .withUserConfiguration(MyConfiguration.class, MyConfigurationOverride.class)
103 | .withPropertyValues("myBean2.enabled")
104 | .run(context -> {
105 | MyBean2 myBean2 = context.getBean(MyBean2.class);
106 | Assertions.assertEquals("I'm MyBean2 overridden", myBean2.me());
107 | });
108 | }
109 |
110 | @Test
111 | public void testOrderMultiple() {
112 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
113 | Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
114 | contextRunner
115 | .withAllowBeanDefinitionOverriding(true)
116 | .withUserConfiguration(MyConfiguration.class, MyConfigurationOverride.class)
117 | .withPropertyValues("myBean2.enabled", "multiple.beans")
118 | .run(context -> {
119 | MyBean2 myBean2 = context.getBean(MyBean2.class);
120 | Assertions.assertEquals("I'm MyBean2 overridden", myBean2.me());
121 | });
122 | });
123 | }
124 |
125 | @Test
126 | public void testMyBean5() {
127 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
128 | contextRunner
129 | .withUserConfiguration(MyConfiguration.class)
130 | .withPropertyValues("myBean5.disabled=false")
131 | .run(context -> {
132 | MyBean5 myBean5 = context.getBean(MyBean5.class);
133 | Assertions.assertEquals("I'm MyBean5", myBean5.me());
134 | });
135 | }
136 |
137 | @Test
138 | public void testMyBean6() {
139 | final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
140 | contextRunner.withUserConfiguration(MyConfiguration.class)
141 | .run(context -> {
142 | MyBean6 myBean6 = context.getBean(MyBean6.class);
143 | Assertions.assertEquals("I'm MyBean6", myBean6.me());
144 | });
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/src/test/java/pl/piomin/services/playground/ConfigurationPropertiesTest.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 | import pl.piomin.services.playground.model.PersonsList;
8 |
9 | @SpringBootTest
10 | public class ConfigurationPropertiesTest {
11 |
12 | @Autowired
13 | PersonsList personsList;
14 |
15 | @Test
16 | public void test() {
17 | Assertions.assertEquals(3, personsList.getPersons().size());
18 | Assertions.assertEquals("John", personsList.getPersons().get(0).getFirstName());
19 | Assertions.assertEquals("Tom", personsList.getPersons().get(1).getFirstName());
20 | Assertions.assertEquals("Kate", personsList.getPersons().get(2).getFirstName());
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/test/java/pl/piomin/services/playground/LoadingOrderFourthTest.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 | import org.springframework.test.context.TestPropertySource;
8 |
9 | @SpringBootTest
10 | @TestPropertySource("classpath:/additional-test.yml")
11 | public class LoadingOrderFourthTest {
12 |
13 | @Value("${property1}")
14 | String property1;
15 | @Value("${property2}")
16 | String property2;
17 | @Value("${property3}")
18 | String property3;
19 | @Value("${property4}")
20 | String property4;
21 |
22 | @Test
23 | public void test() {
24 | Assertions.assertEquals("additional", property4);
25 | Assertions.assertEquals("additional-test", property3);
26 | Assertions.assertEquals("additional-test", property2);
27 | Assertions.assertEquals("app", property1);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/pl/piomin/services/playground/LoadingOrderSecondTest.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 |
8 | @SpringBootTest(properties = {
9 | "spring.config.additional-location=classpath:/sample-appconfig.yml"
10 | })
11 | public class LoadingOrderSecondTest {
12 |
13 | @Value("${property1}")
14 | String property1;
15 | @Value("${property2}")
16 | String property2;
17 | @Value("${property3}")
18 | String property3;
19 |
20 | @Test
21 | public void test() {
22 | Assertions.assertEquals("sample", property3);
23 | Assertions.assertEquals("sample", property2);
24 | Assertions.assertEquals("app", property1);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/pl/piomin/services/playground/LoadingOrderTest.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 |
8 | @SpringBootTest(properties = {
9 | "spring.config.location=classpath:/global.yml,classpath:/override.yml,classpath:/app.yml"
10 | })
11 | public class LoadingOrderTest {
12 |
13 | @Value("${property1}")
14 | String property1;
15 | @Value("${property2}")
16 | String property2;
17 | @Value("${property3}")
18 | String property3;
19 |
20 | @Test
21 | public void test() {
22 | Assertions.assertEquals("app", property3);
23 | Assertions.assertEquals("override", property2);
24 | Assertions.assertEquals("global", property1);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/pl/piomin/services/playground/LoadingOrderThirdTest.java:
--------------------------------------------------------------------------------
1 | package pl.piomin.services.playground;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 |
8 | @SpringBootTest(properties = {
9 | "spring.profiles.active=override"
10 | })
11 | public class LoadingOrderThirdTest {
12 |
13 | @Value("${property1}")
14 | String property1;
15 | @Value("${property2}")
16 | String property2;
17 | @Value("${property3}")
18 | String property3;
19 |
20 | @Test
21 | public void test() {
22 | Assertions.assertEquals("override", property3);
23 | Assertions.assertEquals("override", property2);
24 | Assertions.assertEquals("app", property1);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------