diff --git a/testing-modules/spring-testing-2/src/main/java/com/baeldung/activeprofile/ActiveProfileApplication.java b/testing-modules/spring-testing-2/src/main/java/com/baeldung/activeprofile/ActiveProfileApplication.java new file mode 100644 index 0000000000..19fdff2a0c --- /dev/null +++ b/testing-modules/spring-testing-2/src/main/java/com/baeldung/activeprofile/ActiveProfileApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.activeprofile; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ActiveProfileApplication { + + public static void main(String[] args) { + SpringApplication.run(ActiveProfileApplication.class); + } +} diff --git a/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/DevActiveProfileUnitTest.java b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/DevActiveProfileUnitTest.java new file mode 100644 index 0000000000..dcefddbc3c --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/DevActiveProfileUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.activeprofile; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(classes = ActiveProfileApplication.class) +public class DevActiveProfileUnitTest { + + @Value("${profile.property.value}") + private String propertyString; + + @Test + void whenDevIsActive_thenValueShouldBeKeptFromApplicationYaml() { + Assertions.assertEquals("This the the application.yaml file", propertyString); + } +} diff --git a/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/MultipleActiveProfileUnitTest.java b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/MultipleActiveProfileUnitTest.java new file mode 100644 index 0000000000..f84d0e36b4 --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/MultipleActiveProfileUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.activeprofile; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.EnabledIf; + +@SpringBootTest(classes = ActiveProfileApplication.class) +@EnabledIf(value = "#{{'test', 'prod'}.contains(environment.getActiveProfiles()[0])}", loadContext = true) +@ActiveProfiles(value = "test") +public class MultipleActiveProfileUnitTest { + @Value("${profile.property.value}") + private String propertyString; + + @Autowired + private Environment env; + + @Test + void whenDevIsActive_thenValueShouldBeKeptFromDedicatedApplicationYaml() { + String currentProfile = env.getActiveProfiles()[0]; + Assertions.assertEquals(String.format("This the the application-%s.yaml file", currentProfile), propertyString); + } +} diff --git a/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/ProdActiveProfileUnitTest.java b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/ProdActiveProfileUnitTest.java new file mode 100644 index 0000000000..8427f23960 --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/ProdActiveProfileUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.activeprofile; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.EnabledIf; + +@SpringBootTest(classes = ActiveProfileApplication.class) +@EnabledIf(value = "#{environment.getActiveProfiles()[0] == 'prod'}", loadContext = true) +@ActiveProfiles(value = "prod") +public class ProdActiveProfileUnitTest { + + @Value("${profile.property.value}") + private String propertyString; + + @Test + void whenProdIsActive_thenValueShouldBeKeptFromApplicationProdYaml() { + Assertions.assertEquals("This the the application-prod.yaml file", propertyString); + } + +} diff --git a/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/TestActiveProfileUnitTest.java b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/TestActiveProfileUnitTest.java new file mode 100644 index 0000000000..f17df37050 --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/TestActiveProfileUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.activeprofile; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +@SpringBootTest(classes = ActiveProfileApplication.class) +@ActiveProfiles(value = "test") +public class TestActiveProfileUnitTest { + + @Value("${profile.property.value}") + private String propertyString; + + @Test + void whenTestIsActive_thenValueShouldBeKeptFromApplicationTestYaml() { + Assertions.assertEquals("This the the application-test.yaml file", propertyString); + } +} diff --git a/testing-modules/spring-testing-2/src/test/resources/application-prod.yaml b/testing-modules/spring-testing-2/src/test/resources/application-prod.yaml new file mode 100644 index 0000000000..98afa0421e --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/resources/application-prod.yaml @@ -0,0 +1,3 @@ +profile: + property: + value: This the the application-prod.yaml file \ No newline at end of file diff --git a/testing-modules/spring-testing-2/src/test/resources/application-test.yaml b/testing-modules/spring-testing-2/src/test/resources/application-test.yaml new file mode 100644 index 0000000000..5f183a8d7e --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/resources/application-test.yaml @@ -0,0 +1,3 @@ +profile: + property: + value: This the the application-test.yaml file \ No newline at end of file diff --git a/testing-modules/spring-testing-2/src/test/resources/application.yaml b/testing-modules/spring-testing-2/src/test/resources/application.yaml new file mode 100644 index 0000000000..cf63649116 --- /dev/null +++ b/testing-modules/spring-testing-2/src/test/resources/application.yaml @@ -0,0 +1,6 @@ +spring: + profiles: + active: dev +profile: + property: + value: This the the application.yaml file \ No newline at end of file