From 8cb7ed5847809623ff4d418ca81fd083f2b1bf0b Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 24 Aug 2019 15:05:58 +0300 Subject: [PATCH 1/5] SpringBoot2 - JUnit5 - skip test - reworked using annotations --- springboot2-junit5-skiptest/README.md | 77 +++++++++++++------ .../skiptest/SkiptestApplicationTests.java | 2 +- .../junit/skiptest/TestEnabledCondition.java | 52 ++++++------- 3 files changed, 80 insertions(+), 51 deletions(-) diff --git a/springboot2-junit5-skiptest/README.md b/springboot2-junit5-skiptest/README.md index 3c2fd03..10ef39b 100644 --- a/springboot2-junit5-skiptest/README.md +++ b/springboot2-junit5-skiptest/README.md @@ -69,16 +69,25 @@ public class TestEnabledCondition implements ExecutionCondition { @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class)); - if (annotation.isPresent()) { - String property = annotation.get().property(); - Boolean value = /* ... */; - if (Boolean.TRUE.equals(value)) { - return ConditionEvaluationResult.enabled("Enabled by property: "+property); - } else { - return ConditionEvaluationResult.disabled("Disable by property: "+property); - } - } - return ConditionEvaluationResult.enabled("Enabled by default"); + + return context.getElement() + .map(e -> e.getAnnotation(TestEnabled.class)) + .map(annotation -> { + String property = annotation.property(); + + return Optional.ofNullable(environment.getProperty(property, Boolean.class)) + .map(value -> { + if (Boolean.TRUE.equals(value)) { + return ConditionEvaluationResult.enabled("Enabled by property: "+property); + } else { + return ConditionEvaluationResult.disabled("Disabled by property: "+property); + } + }).orElse( + ConditionEvaluationResult.disabled("Disabled - property <"+property+"> not set!") + ); + }).orElse( + ConditionEvaluationResult.enabled("Enabled by default") + ); } } ``` @@ -166,21 +175,36 @@ public class TestEnabledCondition implements ExecutionCondition { @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { - // ... - String prefix = null; + Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); - Optional classAnnotationPrefix = context.getTestClass().map(cl -> cl.getAnnotation(TestEnabledPrefix.class)); - if (classAnnotationPrefix.isPresent()) { - prefix = classAnnotationPrefix.get().prefix(); - } - - if (prefix != null && !prefix.isEmpty() && !prefix.endsWith(".")) { - prefix += "."; - } else { - prefix = ""; - } - - // ... + return context.getElement() + .map(e -> e.getAnnotation(TestEnabled.class)) + .map(annotation -> { + String property = annotation.property(); + + String prefix = context.getTestClass() + .map(cl -> cl.getAnnotation(TestEnabledPrefix.class)) + .map(pref -> { + if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) { + return pref.prefix()+"."; + } else { + return ""; + } + }).orElse(""); + + return Optional.ofNullable(environment.getProperty(prefix + property, Boolean.class)) + .map(value -> { + if (Boolean.TRUE.equals(value)) { + return ConditionEvaluationResult.enabled("Enabled by property: "+property); + } else { + return ConditionEvaluationResult.disabled("Disabled by property: "+property); + } + }).orElse( + ConditionEvaluationResult.disabled("Disabled - property <"+property+"> not set!") + ); + }).orElse( + ConditionEvaluationResult.enabled("Enabled by default") + ); } } @@ -213,3 +237,8 @@ public class SkiptestApplicationTests { ``` Much more clear and obvious code. + + +## Thanks to... + +1) Reddit user [dpash](https://www.reddit.com/user/dpash/) \ No newline at end of file diff --git a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/SkiptestApplicationTests.java b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/SkiptestApplicationTests.java index ee6c36f..862e5d6 100644 --- a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/SkiptestApplicationTests.java +++ b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/SkiptestApplicationTests.java @@ -6,7 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertTrue; @SpringBootTest -@TestEnabledPrefix(property = "app.skip.test") +@TestEnabledPrefix(prefix = "app.skip.test") public class SkiptestApplicationTests { @TestEnabled(property = "first") diff --git a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java index 9f73836..414d3ad 100644 --- a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java +++ b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java @@ -18,34 +18,34 @@ public class TestEnabledCondition implements ExecutionCondition { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); - Optional annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class)); - if (annotation.isPresent()) { - String prefix = null; + return context.getElement() + .map(e -> e.getAnnotation(TestEnabled.class)) + .map(annotation -> { + String property = annotation.property(); - Optional classAnnotationPrefix = context.getTestClass().map(cl -> cl.getAnnotation(TestEnabledPrefix.class)); - if (classAnnotationPrefix.isPresent()) { - prefix = classAnnotationPrefix.get().prefix(); - } + String prefix = context.getTestClass() + .map(cl -> cl.getAnnotation(TestEnabledPrefix.class)) + .map(pref -> { + if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) { + return pref.prefix()+"."; + } else { + return ""; + } + }).orElse(""); - if (prefix != null && !prefix.isEmpty() && !prefix.endsWith(".")) { - prefix += "."; - } else { - prefix = ""; - } + return Optional.ofNullable(environment.getProperty(prefix + property, Boolean.class)) + .map(value -> { + if (Boolean.TRUE.equals(value)) { + return ConditionEvaluationResult.enabled("Enabled by property: "+property); + } else { + return ConditionEvaluationResult.disabled("Disabled by property: "+property); + } + }).orElse( + ConditionEvaluationResult.disabled("Disabled - property <"+property+"> not set!") + ); + }).orElse( + ConditionEvaluationResult.enabled("Enabled by default") + ); - String property = annotation.get().property(); - if (property.isEmpty()) { - return ConditionEvaluationResult.disabled("Disabled - property not set!"); - } - Boolean value = environment.getProperty(prefix + property, Boolean.class); - if (value == null) { - return ConditionEvaluationResult.disabled("Disabled - property <"+property+"> not set!"); - } else if (Boolean.TRUE.equals(value)) { - return ConditionEvaluationResult.enabled("Enabled by property: "+property); - } else { - return ConditionEvaluationResult.disabled("Disabled by property: "+property); - } - } - return ConditionEvaluationResult.enabled("Enabled by default"); } } From 9576a09aa498fb907a3cd0f598da15e7053fdf67 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 24 Aug 2019 16:14:25 +0300 Subject: [PATCH 2/5] SpringBoot2 - JUnit5 - skip test - next improvement --- springboot2-junit5-skiptest/README.md | 98 ++++++++++++++----- .../junit/skiptest/TestEnabledCondition.java | 62 +++++++----- 2 files changed, 109 insertions(+), 51 deletions(-) diff --git a/springboot2-junit5-skiptest/README.md b/springboot2-junit5-skiptest/README.md index 10ef39b..8777965 100644 --- a/springboot2-junit5-skiptest/README.md +++ b/springboot2-junit5-skiptest/README.md @@ -170,6 +170,60 @@ public @interface TestEnabledPrefix { There is no way avoiding new annotation processing: +### Let's create Annotation Descriptor as follows + +```java +public class TestEnabledCondition implements ExecutionCondition { + + static class AnnotationDescription { + String prefix; + String property; + Boolean value; + AnnotationDescription(String prefix, String property) { + this.prefix = prefix; + this.property = property; + } + String getName() { + return prefix + property; + } + AnnotationDescription setValue(Boolean value) { + this.value = value; + return this; + } + Boolean getValue() { + return value; + } + } + + /* ... */ +} +``` + +It helps us to process annotations using lamdas. + +### Then create a method to extract prefix from context + +```java +public class TestEnabledCondition implements ExecutionCondition { + + /* ... */ + + private AnnotationDescription makeDescription(ExtensionContext context, String property) { + String prefix = context.getTestClass() + .map(cl -> cl.getAnnotation(TestEnabledPrefix.class)) + .map(TestEnabledPrefix::prefix) + .map(pref -> !pref.isEmpty() && !pref.endsWith(".") ? pref + "." : "") + .orElse(""); + return new AnnotationDescription(prefix, property); + } + + /* ... */ + +} +``` + +### And now process the annotation value + ``` java public class TestEnabledCondition implements ExecutionCondition { @@ -178,38 +232,28 @@ public class TestEnabledCondition implements ExecutionCondition { Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); return context.getElement() - .map(e -> e.getAnnotation(TestEnabled.class)) - .map(annotation -> { - String property = annotation.property(); - - String prefix = context.getTestClass() - .map(cl -> cl.getAnnotation(TestEnabledPrefix.class)) - .map(pref -> { - if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) { - return pref.prefix()+"."; - } else { - return ""; - } - }).orElse(""); - - return Optional.ofNullable(environment.getProperty(prefix + property, Boolean.class)) - .map(value -> { - if (Boolean.TRUE.equals(value)) { - return ConditionEvaluationResult.enabled("Enabled by property: "+property); - } else { - return ConditionEvaluationResult.disabled("Disabled by property: "+property); - } - }).orElse( - ConditionEvaluationResult.disabled("Disabled - property <"+property+"> not set!") - ); - }).orElse( - ConditionEvaluationResult.enabled("Enabled by default") - ); + .map(e -> e.getAnnotation(TestEnabled.class)) + .map(TestEnabled::property) + .map(property -> makeDescription(context, property)) + .map(description -> description.setValue(environment.getProperty(description.getName(), Boolean.class))) + .map(description -> { + if (Boolean.TRUE.equals(description.getValue())) { + return ConditionEvaluationResult.enabled("Enabled by property: "+description.getName()); + } else { + return ConditionEvaluationResult.disabled("Disabled by property: "+description.getName()); + } + }).orElse( + ConditionEvaluationResult.enabled("Enabled by default") + ); + } } ``` +## + + You can take a look at [full class code](src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java) folowing to link. ## New annotation usage diff --git a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java index 414d3ad..b0c170b 100644 --- a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java +++ b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.springframework.core.env.Environment; import org.springframework.test.context.junit.jupiter.SpringExtension; -import java.util.Optional; /** * @author bvn13 @@ -14,35 +13,50 @@ import java.util.Optional; */ public class TestEnabledCondition implements ExecutionCondition { + static class AnnotationDescription { + String prefix; + String property; + Boolean value; + AnnotationDescription(String prefix, String property) { + this.prefix = prefix; + this.property = property; + } + String getName() { + return prefix + property; + } + AnnotationDescription setValue(Boolean value) { + this.value = value; + return this; + } + Boolean getValue() { + return value; + } + } + + private AnnotationDescription makeDescription(ExtensionContext context, String property) { + String prefix = context.getTestClass() + .map(cl -> cl.getAnnotation(TestEnabledPrefix.class)) + .map(TestEnabledPrefix::prefix) + .map(pref -> !pref.isEmpty() && !pref.endsWith(".") ? pref + "." : "") + .orElse(""); + return new AnnotationDescription(prefix, property); + } + @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); return context.getElement() .map(e -> e.getAnnotation(TestEnabled.class)) - .map(annotation -> { - String property = annotation.property(); - - String prefix = context.getTestClass() - .map(cl -> cl.getAnnotation(TestEnabledPrefix.class)) - .map(pref -> { - if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) { - return pref.prefix()+"."; - } else { - return ""; - } - }).orElse(""); - - return Optional.ofNullable(environment.getProperty(prefix + property, Boolean.class)) - .map(value -> { - if (Boolean.TRUE.equals(value)) { - return ConditionEvaluationResult.enabled("Enabled by property: "+property); - } else { - return ConditionEvaluationResult.disabled("Disabled by property: "+property); - } - }).orElse( - ConditionEvaluationResult.disabled("Disabled - property <"+property+"> not set!") - ); + .map(TestEnabled::property) + .map(property -> makeDescription(context, property)) + .map(description -> description.setValue(environment.getProperty(description.getName(), Boolean.class))) + .map(description -> { + if (Boolean.TRUE.equals(description.getValue())) { + return ConditionEvaluationResult.enabled("Enabled by property: "+description.getName()); + } else { + return ConditionEvaluationResult.disabled("Disabled by property: "+description.getName()); + } }).orElse( ConditionEvaluationResult.enabled("Enabled by default") ); From bc83dd3c5b63136e7de6753aa7b5efaffdf74a53 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 24 Aug 2019 16:18:24 +0300 Subject: [PATCH 3/5] SpringBoot2 - JUnit5 - skip test - next improvement --- springboot2-junit5-skiptest/README.md | 15 +++++++-------- .../junit/skiptest/TestEnabledCondition.java | 8 +++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/springboot2-junit5-skiptest/README.md b/springboot2-junit5-skiptest/README.md index 8777965..b961432 100644 --- a/springboot2-junit5-skiptest/README.md +++ b/springboot2-junit5-skiptest/README.md @@ -176,15 +176,13 @@ There is no way avoiding new annotation processing: public class TestEnabledCondition implements ExecutionCondition { static class AnnotationDescription { - String prefix; - String property; + String name; Boolean value; AnnotationDescription(String prefix, String property) { - this.prefix = prefix; - this.property = property; + this.name = prefix + property; } String getName() { - return prefix + property; + return name; } AnnotationDescription setValue(Boolean value) { this.value = value; @@ -199,7 +197,7 @@ public class TestEnabledCondition implements ExecutionCondition { } ``` -It helps us to process annotations using lamdas. +It helps us to process annotations using lambdas. ### Then create a method to extract prefix from context @@ -258,7 +256,7 @@ You can take a look at [full class code](src/test/java/com/bvn13/example/springb ## New annotation usage -And now we'll apply new annotation to aour [test class](src/test/java/com/bvn13/example/springboot/junit/skiptest/SkiptestApplicationTests.java): +And now we'll apply new annotation to our [test class](src/test/java/com/bvn13/example/springboot/junit/skiptest/SkiptestApplicationTests.java): ``` java @SpringBootTest @@ -285,4 +283,5 @@ Much more clear and obvious code. ## Thanks to... -1) Reddit user [dpash](https://www.reddit.com/user/dpash/) \ No newline at end of file +1) Reddit user [dpash](https://www.reddit.com/user/dpash/) for [advice](https://www.reddit.com/r/java/comments/cuiqxf/skip_junit_test_according_to_java_springframework/exxz1yr?utm_source=share&utm_medium=web2x) +2) Reddit user [BoyRobot777](https://www.reddit.com/user/BoyRobot777/) for [advice](https://www.reddit.com/r/java/comments/cuiqxf/skip_junit_test_according_to_java_springframework/exy8cnh?utm_source=share&utm_medium=web2x) \ No newline at end of file diff --git a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java index b0c170b..68e7a8d 100644 --- a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java +++ b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java @@ -14,15 +14,13 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; public class TestEnabledCondition implements ExecutionCondition { static class AnnotationDescription { - String prefix; - String property; + String name; Boolean value; AnnotationDescription(String prefix, String property) { - this.prefix = prefix; - this.property = property; + this.name = prefix + property; } String getName() { - return prefix + property; + return name; } AnnotationDescription setValue(Boolean value) { this.value = value; From 33262876559513cf7a482e2698deb385f1df4960 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 24 Aug 2019 16:29:58 +0300 Subject: [PATCH 4/5] SpringBoot2 - JUnit5 - skip test - next improvement --- springboot2-junit5-skiptest/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/springboot2-junit5-skiptest/README.md b/springboot2-junit5-skiptest/README.md index b961432..8db42f4 100644 --- a/springboot2-junit5-skiptest/README.md +++ b/springboot2-junit5-skiptest/README.md @@ -225,6 +225,8 @@ public class TestEnabledCondition implements ExecutionCondition { ``` java public class TestEnabledCondition implements ExecutionCondition { + /* ... */ + @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); From d20d62698d1e31bafad89297dcf2b548e5696fa1 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 24 Aug 2019 17:12:13 +0300 Subject: [PATCH 5/5] SpringBoot2 - JUnit5 - skip test - next improvement --- springboot2-junit5-skiptest/README.md | 34 +++++++++---------- .../junit/skiptest/TestEnabledCondition.java | 14 ++++---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/springboot2-junit5-skiptest/README.md b/springboot2-junit5-skiptest/README.md index 8db42f4..f78bab8 100644 --- a/springboot2-junit5-skiptest/README.md +++ b/springboot2-junit5-skiptest/README.md @@ -176,22 +176,22 @@ There is no way avoiding new annotation processing: public class TestEnabledCondition implements ExecutionCondition { static class AnnotationDescription { - String name; - Boolean value; - AnnotationDescription(String prefix, String property) { - this.name = prefix + property; + String name; + Boolean annotationEnabled; + AnnotationDescription(String prefix, String property) { + this.name = prefix + property; + } + String getName() { + return name; + } + AnnotationDescription setAnnotationEnabled(Boolean value) { + this.annotationEnabled = value; + return this; + } + Boolean isAnnotationEnabled() { + return annotationEnabled; + } } - String getName() { - return name; - } - AnnotationDescription setValue(Boolean value) { - this.value = value; - return this; - } - Boolean getValue() { - return value; - } - } /* ... */ } @@ -235,9 +235,9 @@ public class TestEnabledCondition implements ExecutionCondition { .map(e -> e.getAnnotation(TestEnabled.class)) .map(TestEnabled::property) .map(property -> makeDescription(context, property)) - .map(description -> description.setValue(environment.getProperty(description.getName(), Boolean.class))) + .map(description -> description.setAnnotationEnabled(environment.getProperty(description.getName(), Boolean.class))) .map(description -> { - if (Boolean.TRUE.equals(description.getValue())) { + if (description.isAnnotationEnabled()) { return ConditionEvaluationResult.enabled("Enabled by property: "+description.getName()); } else { return ConditionEvaluationResult.disabled("Disabled by property: "+description.getName()); diff --git a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java index 68e7a8d..d7269aa 100644 --- a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java +++ b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java @@ -15,19 +15,19 @@ public class TestEnabledCondition implements ExecutionCondition { static class AnnotationDescription { String name; - Boolean value; + Boolean annotationEnabled; AnnotationDescription(String prefix, String property) { this.name = prefix + property; } String getName() { return name; } - AnnotationDescription setValue(Boolean value) { - this.value = value; + AnnotationDescription setAnnotationEnabled(Boolean value) { + this.annotationEnabled = value; return this; } - Boolean getValue() { - return value; + Boolean isAnnotationEnabled() { + return annotationEnabled; } } @@ -48,9 +48,9 @@ public class TestEnabledCondition implements ExecutionCondition { .map(e -> e.getAnnotation(TestEnabled.class)) .map(TestEnabled::property) .map(property -> makeDescription(context, property)) - .map(description -> description.setValue(environment.getProperty(description.getName(), Boolean.class))) + .map(description -> description.setAnnotationEnabled(environment.getProperty(description.getName(), Boolean.class))) .map(description -> { - if (Boolean.TRUE.equals(description.getValue())) { + if (description.isAnnotationEnabled()) { return ConditionEvaluationResult.enabled("Enabled by property: "+description.getName()); } else { return ConditionEvaluationResult.disabled("Disabled by property: "+description.getName());