From: Kai Moritz Date: Sun, 14 Aug 2022 10:59:20 +0000 (+0200) Subject: `GenericApplicationTest` überspring Tests, wenn Fehler nicht verfügbar X-Git-Tag: sumup-adder---lvm-2-tage~9^2~6 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=da3b3c96a862aa1408933b312ca965dad1bbe482;p=demos%2Fkafka%2Ftraining `GenericApplicationTest` überspring Tests, wenn Fehler nicht verfügbar * Ü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. --- diff --git a/src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java b/src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java new file mode 100644 index 0000000..6d772ce --- /dev/null +++ b/src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java @@ -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 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 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()); + } +} diff --git a/src/test/java/de/juplo/kafka/GenericApplicationTest.java b/src/test/java/de/juplo/kafka/GenericApplicationTest.java index a6d6aa1..8a57a96 100644 --- a/src/test/java/de/juplo/kafka/GenericApplicationTest.java +++ b/src/test/java/de/juplo/kafka/GenericApplicationTest.java @@ -100,6 +100,7 @@ abstract class GenericApplicationTest } @Test + @SkipWhenErrorCannotBeGenerated(poisonPill = true) void commitsOffsetOfErrorForReprocessingOnDeserializationError() { recordGenerator.generate(100, Set.of(77), Set.of(), messageSender); @@ -133,6 +134,7 @@ abstract class GenericApplicationTest } @Test + @SkipWhenErrorCannotBeGenerated(logicError = true) void doesNotCommitOffsetsOnLogicError() { recordGenerator.generate(100, Set.of(), Set.of(77), messageSender); @@ -241,6 +243,16 @@ abstract class GenericApplicationTest Set poisonPills, Set logicErrors, Consumer> messageSender); + + default boolean canGeneratePoisionPill() + { + return true; + } + + default boolean canGenerateLogicError() + { + return true; + } } void sendMessage(ProducerRecord 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 index 0000000..6d15e9e --- /dev/null +++ b/src/test/java/de/juplo/kafka/SkipWhenErrorCannotBeGenerated.java @@ -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; +}