From cc5b5ab75ca9b52b68c7f1bfba90cb53531ef8f1 Mon Sep 17 00:00:00 2001 From: Vyacheslav Boyko Date: Tue, 30 Jul 2019 12:26:34 +0300 Subject: [PATCH] springboot2-junit5-skiptest update --- .gitignore | 1 + README.md | 4 +- springboot2-junit5-skiptest/README.md | 75 ++++++++++++++++++- .../junit/skiptest/TestEnabledCondition.java | 24 +++++- .../junit/skiptest/TestEnabledPrefix.java | 16 ++++ 5 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledPrefix.java diff --git a/.gitignore b/.gitignore index f5d3313..ace4de0 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ hs_err_pid* # Emacs *~ \#*\# +\.\# diff --git a/README.md b/README.md index 7b9d406..9c86b44 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# JavaLessons \ No newline at end of file +# Java Lessons + +1) (Lesson #1)[springboot2-junit5-skiptest/README.md] - How to write an annotation for your JUnit5 tests and to use application.properties from Spring environment diff --git a/springboot2-junit5-skiptest/README.md b/springboot2-junit5-skiptest/README.md index c0b22e3..cb40599 100644 --- a/springboot2-junit5-skiptest/README.md +++ b/springboot2-junit5-skiptest/README.md @@ -101,7 +101,7 @@ Here is the snippet to obtain Spring environment right from the ExtensionContext Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); ``` -Take a look at [full class code of TestEnabledCondition](src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java) +Take a look at [full class code of TestEnabledCondition](https://github.com/bvn13/JavaLessons/blob/9a34719dbc7b616f0234e4dcd0d5376905aacc2e/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java) # Make some tests @@ -141,4 +141,75 @@ The result: ![](img/result.png) -### Good job! \ No newline at end of file +# Next step - generalizing properties' names + +It is so annoyingly to write the full path to our application properties in every test. + +So the next step is to generalify that path in test class annotation. + +Let's create a new [annotation](src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledPrefix.java) called `TestEnabledPrefix`: + +``` java +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface TestEnabledPrefix { + String prefix(); +} +``` + +## TestEnabledPrefix annotation usage + +There is no way avoiding new annotation processing: + +``` java +public class TestEnabledCondition implements ExecutionCondition { + + @Override + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { + // ... + String prefix = null; + + 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 = ""; + } + + // ... + } + +} +``` + +You can take a look at [full class code](src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledPrefix.java) folowing to link. + +## New annotation usage + +And now we'll apply new annotation to aour test class: + +``` java +@SpringBootTest +@TestEnabledPrefix(property = "app.skip.test") +public class SkiptestApplicationTests { + + @TestEnabled(property = "first") + @Test + public void testFirst() { + assertTrue(true); + } + + @TestEnabled(property = "second") + @Test + public void testSecond() { + assertTrue(false); + } + +} +``` + +Much more clear and obvious code. 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 8736865..9f73836 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 @@ -20,12 +20,30 @@ public class TestEnabledCondition implements ExecutionCondition { Optional annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class)); if (annotation.isPresent()) { + String prefix = null; + + 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 = ""; + } + String property = annotation.get().property(); - Boolean value = environment.getProperty(property, Boolean.class); - if (Boolean.TRUE.equals(value)) { + 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("Disable by property: "+property); + return ConditionEvaluationResult.disabled("Disabled by property: "+property); } } return ConditionEvaluationResult.enabled("Enabled by default"); diff --git a/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledPrefix.java b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledPrefix.java new file mode 100644 index 0000000..2774928 --- /dev/null +++ b/springboot2-junit5-skiptest/src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledPrefix.java @@ -0,0 +1,16 @@ +package com.bvn13.example.springboot.junit.skiptest; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author bvn13 + * @since 26.07.2019 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface TestEnabledPrefix { + String prefix(); +}