SpringBoot2 - JUnit5 - skip test - reworked using annotations

master
bvn13 2019-08-24 15:05:58 +03:00
parent 6bf043f09f
commit 8cb7ed5847
3 changed files with 80 additions and 51 deletions

View File

@ -69,16 +69,25 @@ public class TestEnabledCondition implements ExecutionCondition {
@Override @Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<TestEnabled> annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class)); Optional<TestEnabled> annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class));
if (annotation.isPresent()) {
String property = annotation.get().property(); return context.getElement()
Boolean value = /* ... */; .map(e -> e.getAnnotation(TestEnabled.class))
if (Boolean.TRUE.equals(value)) { .map(annotation -> {
return ConditionEvaluationResult.enabled("Enabled by property: "+property); String property = annotation.property();
} else {
return ConditionEvaluationResult.disabled("Disable by property: "+property); return Optional.ofNullable(environment.getProperty(property, Boolean.class))
} .map(value -> {
} if (Boolean.TRUE.equals(value)) {
return ConditionEvaluationResult.enabled("Enabled by default"); 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 @Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
// ... Environment environment = SpringExtension.getApplicationContext(context).getEnvironment();
String prefix = null;
Optional<TestEnabledPrefix> classAnnotationPrefix = context.getTestClass().map(cl -> cl.getAnnotation(TestEnabledPrefix.class)); return context.getElement()
if (classAnnotationPrefix.isPresent()) { .map(e -> e.getAnnotation(TestEnabled.class))
prefix = classAnnotationPrefix.get().prefix(); .map(annotation -> {
} String property = annotation.property();
if (prefix != null && !prefix.isEmpty() && !prefix.endsWith(".")) { String prefix = context.getTestClass()
prefix += "."; .map(cl -> cl.getAnnotation(TestEnabledPrefix.class))
} else { .map(pref -> {
prefix = ""; 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. Much more clear and obvious code.
## Thanks to...
1) Reddit user [dpash](https://www.reddit.com/user/dpash/)

View File

@ -6,7 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest @SpringBootTest
@TestEnabledPrefix(property = "app.skip.test") @TestEnabledPrefix(prefix = "app.skip.test")
public class SkiptestApplicationTests { public class SkiptestApplicationTests {
@TestEnabled(property = "first") @TestEnabled(property = "first")

View File

@ -18,34 +18,34 @@ public class TestEnabledCondition implements ExecutionCondition {
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Environment environment = SpringExtension.getApplicationContext(context).getEnvironment(); Environment environment = SpringExtension.getApplicationContext(context).getEnvironment();
Optional<TestEnabled> annotation = context.getElement().map(e -> e.getAnnotation(TestEnabled.class)); return context.getElement()
if (annotation.isPresent()) { .map(e -> e.getAnnotation(TestEnabled.class))
String prefix = null; .map(annotation -> {
String property = annotation.property();
Optional<TestEnabledPrefix> classAnnotationPrefix = context.getTestClass().map(cl -> cl.getAnnotation(TestEnabledPrefix.class)); String prefix = context.getTestClass()
if (classAnnotationPrefix.isPresent()) { .map(cl -> cl.getAnnotation(TestEnabledPrefix.class))
prefix = classAnnotationPrefix.get().prefix(); .map(pref -> {
} if (!pref.prefix().isEmpty() && !pref.prefix().endsWith(".")) {
return pref.prefix()+".";
} else {
return "";
}
}).orElse("");
if (prefix != null && !prefix.isEmpty() && !prefix.endsWith(".")) { return Optional.ofNullable(environment.getProperty(prefix + property, Boolean.class))
prefix += "."; .map(value -> {
} else { if (Boolean.TRUE.equals(value)) {
prefix = ""; 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");
} }
} }