Kstreamplify simplifies testing by providing KafkaStreamsStarterTest, which wraps the Kafka Topology Test Driver.
- Add the test dependency: Include
kstreamplify-core-test with test scope. - Create a test class: Extend
KafkaStreamsStarterTest and override getKafkaStreamsStarter() to return your application's starter implementation. - Setup topics: In a
@BeforeEach method, use testDriver.createInputTopic and testDriver.createOutputTopic to define your test streams. Use SerdesUtils to obtain the correct Serdes for your Avro types. - Override properties: If you need to change default test properties (e.g., the state directory), override
getSpecificProperties().
Example test case for verifying business logic and DLQ routing:
<!-- Dependency -->
<dependency>
<groupId>com.michelin</groupId>
<artifactId>kstreamplify-core-test</artifactId>
<version>${kstreamplify.version}</version>
<scope>test</scope>
</dependency>
<!-- Test Implementation -->
public class MyKafkaStreamsTest extends KafkaStreamsStarterTest {
private TestInputTopic<String, KafkaUser> inputTopic;
private TestOutputTopic<String, KafkaUser> outputTopic;
@Override
protected KafkaStreamsStarter getKafkaStreamsStarter() {
return new MyKafkaStreams();
}
@BeforeEach
void setUp() {
inputTopic = testDriver.createInputTopic("input_topic", new StringSerializer(),
SerdesUtils.<KafkaUser>getValueSerdes().serializer());
outputTopic = testDriver.createOutputTopic("output_topic", new StringDeserializer(),
SerdesUtils.<KafkaUser>getValueSerdes().deserializer());
}
@Test
void shouldUpperCase() {
inputTopic.pipeInput("1", user);
List<KeyValue<String, KafkaUser>> results = outputTopic.readKeyValuesToList();
assertEquals("FIRST NAME", results.get(0).value.getFirstName());
}
@Test
void shouldFailAndRouteToDlqTopic() {
inputTopic.pipeInput("1", user);
List<KeyValue<String, KafkaError>> errors = dlqTopic.readKeyValuesToList();
assertEquals("1", errors.get(0).key);
}
@Override
protected Map<String, String> getSpecificProperties() {
return Map.of(STATE_DIR_CONFIG, "/tmp/kafka-streams");
}
}