SpringBoot2 - JUnit5 - skip test - next improvement
parent
8cb7ed5847
commit
9576a09aa4
|
@ -170,6 +170,60 @@ public @interface TestEnabledPrefix {
|
||||||
|
|
||||||
There is no way avoiding new annotation processing:
|
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
|
``` java
|
||||||
public class TestEnabledCondition implements ExecutionCondition {
|
public class TestEnabledCondition implements ExecutionCondition {
|
||||||
|
|
||||||
|
@ -178,38 +232,28 @@ public class TestEnabledCondition implements ExecutionCondition {
|
||||||
Environment environment = SpringExtension.getApplicationContext(context).getEnvironment();
|
Environment environment = SpringExtension.getApplicationContext(context).getEnvironment();
|
||||||
|
|
||||||
return context.getElement()
|
return context.getElement()
|
||||||
.map(e -> e.getAnnotation(TestEnabled.class))
|
.map(e -> e.getAnnotation(TestEnabled.class))
|
||||||
.map(annotation -> {
|
.map(TestEnabled::property)
|
||||||
String property = annotation.property();
|
.map(property -> makeDescription(context, property))
|
||||||
|
.map(description -> description.setValue(environment.getProperty(description.getName(), Boolean.class)))
|
||||||
String prefix = context.getTestClass()
|
.map(description -> {
|
||||||
.map(cl -> cl.getAnnotation(TestEnabledPrefix.class))
|
if (Boolean.TRUE.equals(description.getValue())) {
|
||||||
.map(pref -> {
|
return ConditionEvaluationResult.enabled("Enabled by property: "+description.getName());
|
||||||
if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) {
|
} else {
|
||||||
return pref.prefix()+".";
|
return ConditionEvaluationResult.disabled("Disabled by property: "+description.getName());
|
||||||
} else {
|
}
|
||||||
return "";
|
}).orElse(
|
||||||
}
|
ConditionEvaluationResult.enabled("Enabled by default")
|
||||||
}).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")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##
|
||||||
|
|
||||||
|
|
||||||
You can take a look at [full class code](src/test/java/com/bvn13/example/springboot/junit/skiptest/TestEnabledCondition.java) folowing to link.
|
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
|
## New annotation usage
|
||||||
|
|
|
@ -6,7 +6,6 @@ import org.junit.jupiter.api.extension.ExtensionContext;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bvn13
|
* @author bvn13
|
||||||
|
@ -14,35 +13,50 @@ import java.util.Optional;
|
||||||
*/
|
*/
|
||||||
public class TestEnabledCondition implements ExecutionCondition {
|
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
|
@Override
|
||||||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
||||||
Environment environment = SpringExtension.getApplicationContext(context).getEnvironment();
|
Environment environment = SpringExtension.getApplicationContext(context).getEnvironment();
|
||||||
|
|
||||||
return context.getElement()
|
return context.getElement()
|
||||||
.map(e -> e.getAnnotation(TestEnabled.class))
|
.map(e -> e.getAnnotation(TestEnabled.class))
|
||||||
.map(annotation -> {
|
.map(TestEnabled::property)
|
||||||
String property = annotation.property();
|
.map(property -> makeDescription(context, property))
|
||||||
|
.map(description -> description.setValue(environment.getProperty(description.getName(), Boolean.class)))
|
||||||
String prefix = context.getTestClass()
|
.map(description -> {
|
||||||
.map(cl -> cl.getAnnotation(TestEnabledPrefix.class))
|
if (Boolean.TRUE.equals(description.getValue())) {
|
||||||
.map(pref -> {
|
return ConditionEvaluationResult.enabled("Enabled by property: "+description.getName());
|
||||||
if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) {
|
} else {
|
||||||
return pref.prefix()+".";
|
return ConditionEvaluationResult.disabled("Disabled by property: "+description.getName());
|
||||||
} 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(
|
}).orElse(
|
||||||
ConditionEvaluationResult.enabled("Enabled by default")
|
ConditionEvaluationResult.enabled("Enabled by default")
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue