counter: 1.3.1 - Splitted up test in `CounterStreamProcessorTopologyTest`
authorKai Moritz <kai@juplo.de>
Sun, 16 Jun 2024 20:33:52 +0000 (22:33 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 16 Jun 2024 20:33:52 +0000 (22:33 +0200)
src/test/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessorTopologyTest.java

index cfb6bd8..e80e383 100644 (file)
@@ -11,8 +11,9 @@ import org.apache.kafka.streams.Topology;
 import org.apache.kafka.streams.TopologyTestDriver;
 import org.apache.kafka.streams.state.KeyValueStore;
 import org.apache.kafka.streams.state.Stores;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.springframework.kafka.support.serializer.JsonDeserializer;
 import org.springframework.kafka.support.serializer.JsonSerializer;
@@ -32,13 +33,12 @@ public class CounterStreamProcessorTopologyTest
   public static final String OUT = "TEST-OUT";
 
 
-  TopologyTestDriver testDriver;
-  TestInputTopic<TestInputUser, TestInputWord> in;
-  TestOutputTopic<TestOutputWord, TestOutputWordCounter> out;
+  static TopologyTestDriver testDriver;
+  static MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages = new LinkedMultiValueMap<>();
 
 
-  @BeforeEach
-  public void setUpTestDriver()
+  @BeforeAll
+  public static void setUpTestDriver()
   {
     Topology topology = CounterStreamProcessor.buildTopology(
         IN,
@@ -47,34 +47,53 @@ public class CounterStreamProcessorTopologyTest
 
     testDriver = new TopologyTestDriver(topology, serializationConfig());
 
-    in = testDriver.createInputTopic(IN, serializer(), serializer());
-    out = testDriver.createOutputTopic(OUT, keyDeserializer(), valueDeserializer());
-  }
-
+    TestInputTopic<TestInputUser, TestInputWord> in =
+        testDriver.createInputTopic(IN, serializer(), serializer());
+    TestOutputTopic<TestOutputWord, TestOutputWordCounter> out =
+        testDriver.createOutputTopic(OUT, keyDeserializer(), valueDeserializer());
 
-  @Test
-  public void test()
-  {
     TestData
         .getInputMessages()
         .forEach(kv -> in.pipeInput(kv.key, kv.value));
 
-    MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages = new LinkedMultiValueMap<>();
+    receivedMessages = new LinkedMultiValueMap<>();
     out
         .readRecordsToList()
         .forEach(record -> receivedMessages.add(record.key(), record.value()));
+  }
 
+
+  @DisplayName("Assert the expected output messages")
+  @Test
+  public void testExpectedMessages()
+  {
     TestData.assertExpectedMessages(receivedMessages);
+  }
 
+  @DisplayName("Assert the expected number of messages")
+  @Test
+  public void testExpectedNumberOfMessagesForWord()
+  {
     TestData.assertExpectedNumberOfMessagesForWord(receivedMessages);
+  }
+
+  @DisplayName("Await the expected final output messages")
+  @Test
+  public void testExpectedLastMessagesForWord()
+  {
     TestData.assertExpectedLastMessagesForWord(receivedMessages);
+  }
 
+  @DisplayName("Assert the expected state in the state-store")
+  @Test
+  public void testExpectedState()
+  {
     KeyValueStore<Word, Long> store = testDriver.getKeyValueStore(STORE_NAME);
     TestData.assertExpectedState(store);
   }
 
-  @AfterEach
-  public void tearDown()
+  @AfterAll
+  public static void tearDown()
   {
     testDriver.close();
   }
@@ -85,7 +104,7 @@ public class CounterStreamProcessorTopologyTest
     return new JsonSerializer().noTypeInfo();
   }
 
-  private JsonDeserializer<TestOutputWord> keyDeserializer()
+  private static JsonDeserializer<TestOutputWord> keyDeserializer()
   {
     return deserializer(true);
   }