From ce80343050a33118783dda13f530732153c3271d Mon Sep 17 00:00:00 2001 From: azhwani <> Date: Sat, 4 Jul 2020 11:57:52 +0100 Subject: [PATCH 1/6] Add YamlListApplication class --- .../properties/yamllist/YamlListApplication.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/YamlListApplication.java diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/YamlListApplication.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/YamlListApplication.java new file mode 100644 index 0000000000..c8fa1af22e --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/YamlListApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.properties.yamllist; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class YamlListApplication { + + public static void main(String[] args) { + SpringApplication.run(YamlListApplication.class, args); + } + +} From 126233cf6c6a2d0085fd48a975c438eb0fd5cf42 Mon Sep 17 00:00:00 2001 From: azhwani <> Date: Sat, 4 Jul 2020 11:57:59 +0100 Subject: [PATCH 2/6] Add ApplicationProps POJO --- .../yamllist/pojo/ApplicationProps.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java new file mode 100644 index 0000000000..18a4d569b9 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java @@ -0,0 +1,71 @@ +package com.baeldung.properties.yamllist.pojo; + +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "application") +public class ApplicationProps { + + private List profiles; + private List> team; + private List users; + + public List getProfiles() { + return profiles; + } + + public void setProfiles(List profiles) { + this.profiles = profiles; + } + + public List> getTeam() { + return team; + } + + public void setTeam(List> team) { + this.team = team; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + public static class User { + + private String username; + private String password; + private List roles; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + } +} \ No newline at end of file From 8f99163df7aba433d8b3b25651cdde1eabb20c94 Mon Sep 17 00:00:00 2001 From: azhwani <> Date: Sat, 4 Jul 2020 11:58:07 +0100 Subject: [PATCH 3/6] Update application.yml --- .../src/main/resources/application.yml | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml index 51ba814a97..57b5693519 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml @@ -20,3 +20,34 @@ server: guest: username: guest password: guestpass +--- +application: + profiles: + - dev + - test + - prod + - 1 + - 2 + team: + - + - James + - Matthew + - Olivia + - + - Natalie + - Brittany + - Christian + users: + - + username: admin + password: admin@10@ + roles: + - READ + - WRITE + - VIEW + - DELETE + - + username: guest + password: guest@01 + roles: + - VIEW \ No newline at end of file From a1732d99a70942dcf95e315c5e46b1384f12dd85 Mon Sep 17 00:00:00 2001 From: azhwani <> Date: Sat, 4 Jul 2020 11:58:20 +0100 Subject: [PATCH 4/6] Create test classes --- .../YamlComplexListsIntegrationTest.java | 30 ++++++++++++++++++ .../YamlSimpleListIntegrationTest.java | 31 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java new file mode 100644 index 0000000000..7e33d66917 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java @@ -0,0 +1,30 @@ +package com.baeldung.properties.yamllist; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.properties.yamllist.pojo.ApplicationProps; + +@RunWith(SpringRunner.class) +@SpringBootTest +class YamlComplexListsIntegrationTest { + + @Autowired + private ApplicationProps applicationProps; + + @Test + public void whenYamlNestedLists_thenLoadComplexLists() { + assertThat(applicationProps.getUsers() + .get(0) + .getPassword()).isEqualTo("admin@10@"); + assertThat(applicationProps.getTeam() + .get(0) + .size()).isEqualTo(3); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java new file mode 100644 index 0000000000..119733b9a3 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.properties.yamllist; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.properties.yamllist.pojo.ApplicationProps; + +@RunWith(SpringRunner.class) +@SpringBootTest +class YamlSimpleListIntegrationTest { + + @Autowired + private ApplicationProps applicationProps; + + @Test + public void whenYamlList_thenLoadSimpleList() { + assertThat(applicationProps.getProfiles() + .get(0)).isEqualTo("dev"); + assertThat(applicationProps.getProfiles() + .get(4) + .getClass() + .toString()).isEqualTo("class java.lang.Integer"); + assertThat(applicationProps.getProfiles() + .size()).isEqualTo(5); + } +} From aeb1653c89767aa15c3d40b7e5ccc69585705170 Mon Sep 17 00:00:00 2001 From: azhwani <> Date: Tue, 7 Jul 2020 12:00:37 +0100 Subject: [PATCH 5/6] quick fix --- .../yamllist/pojo/ApplicationProps.java | 11 ++++++----- .../src/main/resources/application.yml | 16 +++++++++------- .../YamlComplexListsIntegrationTest.java | 8 ++++++-- .../yamllist/YamlSimpleListIntegrationTest.java | 3 +-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java index 18a4d569b9..f05e3cbbff 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamllist/pojo/ApplicationProps.java @@ -1,6 +1,7 @@ package com.baeldung.properties.yamllist.pojo; import java.util.List; +import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -10,7 +11,7 @@ import org.springframework.stereotype.Component; public class ApplicationProps { private List profiles; - private List> team; + private List> props; private List users; public List getProfiles() { @@ -21,12 +22,12 @@ public class ApplicationProps { this.profiles = profiles; } - public List> getTeam() { - return team; + public List> getProps() { + return props; } - public void setTeam(List> team) { - this.team = team; + public void setProps(List> props) { + this.props = props; } public List getUsers() { diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml index 57b5693519..b1ea0a859b 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/application.yml @@ -28,15 +28,17 @@ application: - prod - 1 - 2 - team: + props: - - - James - - Matthew - - Olivia + name: YamlList + url: http://yamllist.dev + description: Mapping list in Yaml to list of objects in Spring Boot - - - Natalie - - Brittany - - Christian + ip: 10.10.10.10 + port: 8091 + - + email: support@yamllist.dev + contact: http://yamllist.dev/contact users: - username: admin diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java index 7e33d66917..b0dc881f2a 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java @@ -22,9 +22,13 @@ class YamlComplexListsIntegrationTest { assertThat(applicationProps.getUsers() .get(0) .getPassword()).isEqualTo("admin@10@"); - assertThat(applicationProps.getTeam() + assertThat(applicationProps.getProps() .get(0) - .size()).isEqualTo(3); + .get("name")).isEqualTo("YamlList"); + assertThat(applicationProps.getProps() + .get(1) + .get("port") + .getClass()).isEqualTo(Integer.class); } } diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java index 119733b9a3..888ffd13f8 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java @@ -23,8 +23,7 @@ class YamlSimpleListIntegrationTest { .get(0)).isEqualTo("dev"); assertThat(applicationProps.getProfiles() .get(4) - .getClass() - .toString()).isEqualTo("class java.lang.Integer"); + .getClass()).isEqualTo(Integer.class); assertThat(applicationProps.getProfiles() .size()).isEqualTo(5); } From d0271aaf8e4dec09a7ccfbf554ed533aee7d82c2 Mon Sep 17 00:00:00 2001 From: azhwani <> Date: Thu, 9 Jul 2020 13:16:27 +0100 Subject: [PATCH 6/6] quick fix --- ...ionTest.java => YamlComplexListsUnitTest.java} | 15 +++++++++------ ...ationTest.java => YamlSimpleListUnitTest.java} | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) rename spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/{YamlComplexListsIntegrationTest.java => YamlComplexListsUnitTest.java} (56%) rename spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/{YamlSimpleListIntegrationTest.java => YamlSimpleListUnitTest.java} (55%) diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsUnitTest.java similarity index 56% rename from spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java rename to spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsUnitTest.java index b0dc881f2a..6dc5d61d09 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlComplexListsUnitTest.java @@ -3,16 +3,19 @@ package com.baeldung.properties.yamllist; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import com.baeldung.properties.yamllist.pojo.ApplicationProps; -@RunWith(SpringRunner.class) -@SpringBootTest -class YamlComplexListsIntegrationTest { +@ExtendWith(SpringExtension.class) +@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) +@EnableConfigurationProperties(value = ApplicationProps.class) +class YamlComplexListsUnitTest { @Autowired private ApplicationProps applicationProps; diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListUnitTest.java similarity index 55% rename from spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java rename to spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListUnitTest.java index 888ffd13f8..475a73c7d7 100644 --- a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamllist/YamlSimpleListUnitTest.java @@ -3,16 +3,19 @@ package com.baeldung.properties.yamllist; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import com.baeldung.properties.yamllist.pojo.ApplicationProps; -@RunWith(SpringRunner.class) -@SpringBootTest -class YamlSimpleListIntegrationTest { +@ExtendWith(SpringExtension.class) +@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) +@EnableConfigurationProperties(value = ApplicationProps.class) +class YamlSimpleListUnitTest { @Autowired private ApplicationProps applicationProps;