Erzeugung des `ExampleConsumer` im Tests über Methode konfigurierbar
authorKai Moritz <kai@juplo.de>
Sun, 22 Dec 2024 18:03:22 +0000 (19:03 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 5 Jan 2025 11:26:36 +0000 (12:26 +0100)
src/test/java/de/juplo/kafka/ExampleConsumerTest.java

index ec874c4..4394e9a 100644 (file)
@@ -23,6 +23,7 @@ import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.util.backoff.BackOff;
 import org.springframework.util.backoff.FixedBackOff;
 
 import java.time.Clock;
@@ -59,6 +60,8 @@ public class ExampleConsumerTest
   @Test
   void testOnlyValidMessages()
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -90,6 +93,8 @@ public class ExampleConsumerTest
   @Test
   void testDeserializationException()
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -121,6 +126,8 @@ public class ExampleConsumerTest
   @Test
   void testUnexpectedDomainError() throws Exception
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -153,6 +160,8 @@ public class ExampleConsumerTest
   @Test
   void testNonRetryableDomainError() throws Exception
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -185,6 +194,8 @@ public class ExampleConsumerTest
   @ValueSource(ints = { 1, 2, 3, 4, 5, 6 })
   void testOneMessageCausesRetryableDomainErrors(int numFailures)
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -216,6 +227,8 @@ public class ExampleConsumerTest
   @Test
   void testOneMessageCausesRetryableDomainErrors()
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -251,6 +264,8 @@ public class ExampleConsumerTest
     int numFailuresForMessageB,
     int numFailuresForMessageC)
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -296,6 +311,8 @@ public class ExampleConsumerTest
     int numFailuresForMessageB,
     int numFailuresForMessageC)
   {
+    createExampleConsumer();
+
     sendValidMessage(0);
     sendValidMessage(1);
     sendValidMessage(2);
@@ -344,6 +361,8 @@ public class ExampleConsumerTest
 
   @Autowired
   KafkaTemplate<String, byte[]> kafkaTemplate;
+  @Autowired ApplicationProperties properties;
+  @Autowired Clock clock;
 
   final LongSerializer serializer = new LongSerializer();
   final long[] currentOffsets = new long[NUM_PARTITIONS];
@@ -356,10 +375,12 @@ public class ExampleConsumerTest
   ExampleConsumer exampleConsumer;
 
 
-  @BeforeEach
-  void createExampleConsumer(
-    @Autowired ApplicationProperties properties,
-    @Autowired Clock clock)
+  void createExampleConsumer()
+  {
+    createExampleConsumer(new FixedBackOff(0l, properties.getConsumerProperties().getNumRetries()));
+  }
+
+  void createExampleConsumer(BackOff backOff)
   {
     ApplicationConfiguration configuration = new ApplicationConfiguration();
     Consumer<String, Long> consumer = configuration.kafkaConsumer(properties);
@@ -373,7 +394,7 @@ public class ExampleConsumerTest
       properties.getConsumerProperties().getPollRequestTimeout(),
       properties.getConsumerProperties().getMaxPollInterval(),
       properties.getConsumerProperties().getMaxTimePerRecord(),
-      new FixedBackOff(0l, properties.getConsumerProperties().getNumRetries()),
+      backOff,
       () -> isTerminatedExceptionally.set(true));
   }