From 085f8447a9a82ccc799340353eb8aa4ab18efa62 Mon Sep 17 00:00:00 2001 From: Saritha Rani Yellanki Date: Tue, 30 Oct 2018 21:21:47 +0530 Subject: [PATCH 1/2] Adding Sample Property Loading Test class --- .../mock/SamplePropertyLoadingTest .java | 29 +++++++++++++++++++ src/test/resources/application.yml | 12 ++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java diff --git a/src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java b/src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java new file mode 100644 index 0000000..cedddfb --- /dev/null +++ b/src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java @@ -0,0 +1,29 @@ +package br.com.elementalsource.mock; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = SampleWebApplication.class) +public class SamplePropertyLoadingTest { + @Autowired + private SampleProperty sampleProperty; + @Value("${prefix.stringProp1}") + private String stringProp1; + + @Test + public void testLoadingOfProperties() { + System.out.println("stringProp1 = " + stringProp1); + assertThat(sampleProperty.getStringProp1(), equalTo("propValue1")); + assertThat(sampleProperty.getStringProp2(), equalTo("propValue2")); + assertThat(sampleProperty.getIntProp1(), equalTo(10)); + assertThat(sampleProperty.getListProp(), hasItems("listValue1", "listValue2")); + assertThat(sampleProperty.getMapProp(), allOf(hasEntry("key1", "mapValue1"), hasEntry("key2", "mapValue2"))); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 02d313e..1e8d791 100755 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -39,3 +39,15 @@ captureState: false #debug: true logging.level.ROOT: INFO + +prefix: + stringProp1: propValue1 + stringProp2: propValue2 + intProp1: 10 + listProp: + - listValue1 + - listValue2 + mapProp: + key1: mapValue1 + key2: mapValue2 + From cbb6975bc6e3767d9b81062218304bced1a8fb33 Mon Sep 17 00:00:00 2001 From: Saritha Rani Yellanki Date: Tue, 30 Oct 2018 22:12:48 +0530 Subject: [PATCH 2/2] Adding sample property file loading class --- .../SamplePropertyLoadingController.java | 22 ++++++++++++++ .../mock/SamplePropertyLoadingTest .java | 29 ------------------- src/test/resources/application.yml | 12 -------- 3 files changed, 22 insertions(+), 41 deletions(-) create mode 100644 src/main/java/br/com/elementalsource/mock/configuration/api/v1/controller/SamplePropertyLoadingController.java delete mode 100644 src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java diff --git a/src/main/java/br/com/elementalsource/mock/configuration/api/v1/controller/SamplePropertyLoadingController.java b/src/main/java/br/com/elementalsource/mock/configuration/api/v1/controller/SamplePropertyLoadingController.java new file mode 100644 index 0000000..86e7292 --- /dev/null +++ b/src/main/java/br/com/elementalsource/mock/configuration/api/v1/controller/SamplePropertyLoadingController.java @@ -0,0 +1,22 @@ +package br.com.elementalsource.mock.configuration.api.v1.controller; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties") +@Controller +public class SamplePropertyLoadingController { + + @Value("${myName}") + String name; + + @RequestMapping(value = "/sampleProperty") + @ResponseBody + public void getName() { + System.out.println(name); + } + +} diff --git a/src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java b/src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java deleted file mode 100644 index cedddfb..0000000 --- a/src/test/java/br/com/elementalsource/mock/SamplePropertyLoadingTest .java +++ /dev/null @@ -1,29 +0,0 @@ -package br.com.elementalsource.mock; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = SampleWebApplication.class) -public class SamplePropertyLoadingTest { - @Autowired - private SampleProperty sampleProperty; - @Value("${prefix.stringProp1}") - private String stringProp1; - - @Test - public void testLoadingOfProperties() { - System.out.println("stringProp1 = " + stringProp1); - assertThat(sampleProperty.getStringProp1(), equalTo("propValue1")); - assertThat(sampleProperty.getStringProp2(), equalTo("propValue2")); - assertThat(sampleProperty.getIntProp1(), equalTo(10)); - assertThat(sampleProperty.getListProp(), hasItems("listValue1", "listValue2")); - assertThat(sampleProperty.getMapProp(), allOf(hasEntry("key1", "mapValue1"), hasEntry("key2", "mapValue2"))); - } -} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 1e8d791..02d313e 100755 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -39,15 +39,3 @@ captureState: false #debug: true logging.level.ROOT: INFO - -prefix: - stringProp1: propValue1 - stringProp2: propValue2 - intProp1: 10 - listProp: - - listValue1 - - listValue2 - mapProp: - key1: mapValue1 - key2: mapValue2 -