`GenericApplicationTest` überspring Tests, wenn Fehler nicht verfügbar
authorKai Moritz <kai@juplo.de>
Sun, 14 Aug 2022 10:59:20 +0000 (12:59 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 14 Aug 2022 11:01:06 +0000 (13:01 +0200)
* Über eine Annotation wird für Tests, die einen bestimmten Fehler-Typ
  benötigen bei dem `RecordGenerator` nachgefragt, ob der Fehler-Typ
  erzeugt werden kann.
* Wenn der Fehler-Typ nicht zur Verfügung steht, wird der Test
  übersprungen.

src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/GenericApplicationTest.java
src/test/java/de/juplo/kafka/SkipWhenErrorCannotBeGenerated.java [new file with mode: 0644]

diff --git a/src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java b/src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java
new file mode 100644 (file)
index 0000000..6d772ce
--- /dev/null
@@ -0,0 +1,60 @@
+package de.juplo.kafka;
+
+import org.junit.jupiter.api.extension.ConditionEvaluationResult;
+import org.junit.jupiter.api.extension.ExecutionCondition;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.platform.commons.util.AnnotationUtils;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+
+public class ErrorCannotBeGeneratedCondition implements ExecutionCondition
+{
+  @Override
+  public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context)
+  {
+    final Optional<SkipWhenErrorCannotBeGenerated> optional =
+        AnnotationUtils.findAnnotation(
+            context.getElement(),
+            SkipWhenErrorCannotBeGenerated.class);
+
+    if (context.getTestInstance().isEmpty())
+      return ConditionEvaluationResult.enabled("Test-instance ist not available");
+
+    if (optional.isPresent())
+    {
+      SkipWhenErrorCannotBeGenerated skipWhenErrorCannotBeGenerated = optional.get();
+      GenericApplicationTest instance = (GenericApplicationTest)context.getTestInstance().get();
+      List<String> missingRequiredErrors = new LinkedList<>();
+
+      if (skipWhenErrorCannotBeGenerated.poisonPill() && !instance.recordGenerator.canGeneratePoisionPill())
+        missingRequiredErrors.add("Poison-Pill");
+
+      if (skipWhenErrorCannotBeGenerated.logicError() && !instance.recordGenerator.canGenerateLogicError())
+        missingRequiredErrors.add("Logic-Error");
+
+      StringBuilder builder = new StringBuilder();
+      builder.append(context.getTestClass().get().getSimpleName());
+
+      if (missingRequiredErrors.isEmpty())
+      {
+        builder.append(" can generate all required types of errors");
+        return ConditionEvaluationResult.enabled(builder.toString());
+      }
+
+      builder.append(" cannot generate the required error(s): ");
+      builder.append(
+          missingRequiredErrors
+              .stream()
+              .collect(Collectors.joining(", ")));
+
+      return ConditionEvaluationResult.disabled(builder.toString());
+    }
+
+    return ConditionEvaluationResult.enabled(
+        "Not annotated with " + SkipWhenErrorCannotBeGenerated.class.getSimpleName());
+  }
+}
index a6d6aa1..8a57a96 100644 (file)
@@ -100,6 +100,7 @@ abstract class GenericApplicationTest<K, V>
        }
 
        @Test
+       @SkipWhenErrorCannotBeGenerated(poisonPill = true)
        void commitsOffsetOfErrorForReprocessingOnDeserializationError()
        {
                recordGenerator.generate(100, Set.of(77), Set.of(), messageSender);
@@ -133,6 +134,7 @@ abstract class GenericApplicationTest<K, V>
        }
 
        @Test
+       @SkipWhenErrorCannotBeGenerated(logicError = true)
        void doesNotCommitOffsetsOnLogicError()
        {
                recordGenerator.generate(100, Set.of(), Set.of(77), messageSender);
@@ -241,6 +243,16 @@ abstract class GenericApplicationTest<K, V>
                                Set<Integer> poisonPills,
                                Set<Integer> logicErrors,
                                Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
+
+               default boolean canGeneratePoisionPill()
+               {
+                       return true;
+               }
+
+               default boolean canGenerateLogicError()
+               {
+                       return true;
+               }
        }
 
        void sendMessage(ProducerRecord<Bytes, Bytes> record)
diff --git a/src/test/java/de/juplo/kafka/SkipWhenErrorCannotBeGenerated.java b/src/test/java/de/juplo/kafka/SkipWhenErrorCannotBeGenerated.java
new file mode 100644 (file)
index 0000000..6d15e9e
--- /dev/null
@@ -0,0 +1,15 @@
+package de.juplo.kafka;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+
+@Retention(RetentionPolicy.RUNTIME)
+@ExtendWith(ErrorCannotBeGeneratedCondition.class)
+public @interface SkipWhenErrorCannotBeGenerated
+{
+  boolean poisonPill() default false;
+  boolean logicError() default false;
+}