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"); } }