Verwendung eines weniger verwirrenden Key in Tests (fachlich irrelevant)
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index 92074ff..05eebd0 100644 (file)
@@ -31,10 +31,8 @@ import java.util.stream.IntStream;
 
 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
 import static de.juplo.kafka.ApplicationTests.TOPIC;
-import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.*;
 import static org.awaitility.Awaitility.*;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertThrows;
 
 
 @SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
@@ -79,7 +77,11 @@ class ApplicationTests
        @Order(1) // << The poistion pill is not skipped. Hence, this test must run first
        void commitsCurrentOffsetsOnSuccess() throws ExecutionException, InterruptedException
        {
-               send100Messages(i ->  new Bytes(valueSerializer.serialize(TOPIC, i)));
+               send100Messages((partition, key, counter) ->
+               {
+                       Bytes value = new Bytes(valueSerializer.serialize(TOPIC, counter));
+                       return new ProducerRecord<>(TOPIC, partition, key, value);
+               });
 
                await("100 records received")
                                .atMost(Duration.ofSeconds(30))
@@ -93,20 +95,22 @@ class ApplicationTests
                                        compareToCommitedOffsets(newOffsets);
                                });
 
-               assertThrows(
-                               IllegalStateException.class,
-                               () -> endlessConsumer.exitStatus(),
-                               "Consumer should still be running");
+               assertThatExceptionOfType(IllegalStateException.class)
+                               .isThrownBy(() -> endlessConsumer.exitStatus())
+                               .describedAs("Consumer should still be running");
        }
 
        @Test
        @Order(2)
        void commitsOffsetOfErrorForReprocessingOnError()
        {
-               send100Messages(counter ->
-                               counter == 77
-                                               ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
-                                               : new Bytes(valueSerializer.serialize(TOPIC, counter)));
+               send100Messages((partition, key, counter) ->
+               {
+                       Bytes value = counter == 77
+                                       ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
+                                       : new Bytes(valueSerializer.serialize(TOPIC, counter));
+                       return new ProducerRecord<>(TOPIC, partition, key, value);
+               });
 
                await("Consumer failed")
                                .atMost(Duration.ofSeconds(30))
@@ -126,9 +130,9 @@ class ApplicationTests
                                .describedAs("Received not all sent events")
                                .isLessThan(100);
 
-               assertDoesNotThrow(
-                               () -> endlessConsumer.exitStatus(),
-                               "Consumer should not be running");
+               assertThatNoException()
+                               .describedAs("Consumer should not be running")
+                               .isThrownBy(() -> endlessConsumer.exitStatus());
                assertThat(endlessConsumer.exitStatus())
                                .describedAs("Consumer should have exited abnormally")
                                .containsInstanceOf(RecordDeserializationException.class);
@@ -188,7 +192,12 @@ class ApplicationTests
        }
 
 
-       void send100Messages(Function<Long, Bytes> messageGenerator)
+       public interface RecordGenerator<K, V>
+       {
+               public ProducerRecord<String, Bytes> generate(int partition, String key, long counter);
+       }
+
+       void send100Messages(RecordGenerator recordGenerator)
        {
                long i = 0;
 
@@ -196,14 +205,8 @@ class ApplicationTests
                {
                        for (int key = 0; key < 10; key++)
                        {
-                               Bytes value = messageGenerator.apply(++i);
-
                                ProducerRecord<String, Bytes> record =
-                                               new ProducerRecord<>(
-                                                               TOPIC,
-                                                               partition,
-                                                               Integer.toString(key%2),
-                                                               value);
+                                               recordGenerator.generate(partition, Integer.toString(partition*10+key%2), ++i);
 
                                kafkaProducer.send(record, (metadata, e) ->
                                {