From 9576a09aa498fb907a3cd0f598da15e7053fdf67 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 24 Aug 2019 16:14:25 +0300 Subject: [PATCH] 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") );