springboot2-junit5-skiptest update

master
Vyacheslav Boyko 2019-07-30 12:26:34 +03:00
parent e6d3ce85d3
commit cc5b5ab75c
5 changed files with 114 additions and 6 deletions

1
.gitignore vendored
View File

@ -25,3 +25,4 @@ hs_err_pid*
# Emacs
*~
\#*\#
\.\#

View File

@ -1 +1,3 @@
# JavaLessons
# 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

View File

@ -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!
# 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<TestEnabledPrefix> 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.

View File

@ -20,12 +20,30 @@ public class TestEnabledCondition implements ExecutionCondition {
Optional<TestEnabled> annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class));
if (annotation.isPresent()) {
String prefix = null;
Optional<TestEnabledPrefix> 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");

View File

@ -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();
}