From b3b69272fad58705b48c5e40be99516a79d63531 Mon Sep 17 00:00:00 2001 From: cesarevalenti90 <50798547+cesarevalenti90@users.noreply.github.com> Date: Thu, 11 May 2023 00:09:10 +0200 Subject: [PATCH] BAEL-6197 - Execute Tests Based on Active Profile with JUnit 5 (#13776) * BAEL-6255 - Run a Spring Boot application in AWS Lambda * BAEL-6255 - Run a Spring Boot application in AWS Lambda * fix on template.yaml * fix on template.yaml * removed log from test * resolved issues reported on PR * BAEL-6277 - A Guide To Spring Cloud Azure First commit * BAEL-6277 - A Guide To Spring Cloud Azure Added to README.md the steps to create the secrets * BAEL-6277 - A Guide To Spring Cloud Azure Added the integration Azure Key Vault Properties * BAEL-6277 - A Guide To Spring Cloud Azure Added the integration Azure Key Vault Properties * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault Added one level package keyvault * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault removed target release version * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault fix compilation of NoSuchElementException * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault fix pom.xml * Revert "BAEL-6277 - A Guide To Spring Cloud Azure Key Vault" This reverts commit 1cca1d0d692646001a6d7de106f3a37fb22839ce. * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault fix pom.xml * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault downgrade version to fix jenkins pipeline error * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault comment run on main class * Revert "reset last commit" This reverts commit c557fad67f1c15bb384f0af5781430a45121a838. * BAEL 6197 - Execute Tests Based on Active Profile with JUnit 5 #First push * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault Revert for new pr * reset last commit * reset last commit * BAEL 6197 - Execute Tests Based on Active Profile with JUnit 5 #Remove redundant test * BAEL 6197 - Execute Tests Based on Active Profile with JUnit 5 #Format fix * BAEL 6197 - Execute Tests Based on Active Profile with JUnit 5 #fix from review * BAEL 6197 - Execute Tests Based on Active Profile with JUnit 5 #fix from review * BAEL 6197 - Execute Tests Based on Active Profile with JUnit 5 #fix test name --------- Co-authored-by: Cesare --- .../ActiveProfileApplication.java | 12 +++++++++ .../DevActiveProfileUnitTest.java | 18 +++++++++++++ .../MultipleActiveProfileUnitTest.java | 27 +++++++++++++++++++ .../ProdActiveProfileUnitTest.java | 23 ++++++++++++++++ .../TestActiveProfileUnitTest.java | 20 ++++++++++++++ .../src/test/resources/application-prod.yaml | 3 +++ .../src/test/resources/application-test.yaml | 3 +++ .../src/test/resources/application.yaml | 6 +++++ 8 files changed, 112 insertions(+) create mode 100644 testing-modules/spring-testing-2/src/main/java/com/baeldung/activeprofile/ActiveProfileApplication.java create mode 100644 testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/DevActiveProfileUnitTest.java create mode 100644 testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/MultipleActiveProfileUnitTest.java create mode 100644 testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/ProdActiveProfileUnitTest.java create mode 100644 testing-modules/spring-testing-2/src/test/java/com/baeldung/activeprofile/TestActiveProfileUnitTest.java create mode 100644 testing-modules/spring-testing-2/src/test/resources/application-prod.yaml create mode 100644 testing-modules/spring-testing-2/src/test/resources/application-test.yaml create mode 100644 testing-modules/spring-testing-2/src/test/resources/application.yaml 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