liveBook · Manning

Test

As explained in Kafka Streams in Action, Second Edition

Testing is the systematic evaluation of software behavior against expected results under defined inputs and conditions.

Testing evaluates expected behavior across defined inputs and scenarios, proving an application's contract and catching regressions during change or refactoring. Fast, repeatable tests belong in continuous integration; Kafka applications should keep business logic independent of event-streaming infrastructure so it can be tested directly. section 14.0

Unit tests isolate a specific component, usually method-level logic, and use mocks to supply inputs and validate results without real external services. Integration tests instead run real connected components to verify that the system works together; they are slower, so they should be fewer. section 14.1

Test a producer-consumer application without a broker

A currency-exchange client consumes non-US transactions, converts each amount to US dollars, and produces the result. Accept Consumer and Producer interfaces in its constructor, substitute MockConsumer and MockProducer, schedule setup and input-record tasks, then schedule close to end the polling loop. Inspect producer history and assert the converted transactions match expectations. section 14.1.1

For Kafka Streams, test mappers, filters, aggregators, and punctuators as concrete units, then exercise the complete topology with TopologyTestDriver. Create test input and output topics, pipe records through the extracted production topology, read output, and compare it with expected values. Supply timestamps when window advancement matters. section 14.1.3 section 14.2

Use integration tests when a real broker is necessary. Testcontainers manages the Kafka container lifecycle; with the JUnit 5 extension, making the container field static can avoid starting a new container for every test in a class. section 14.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Walks through mock-based client testing and full-topology testing, while deferring broader integration-test implementation details.
Examples
Uses a currency-exchange client and a Yelling Kafka Streams topology that uppercases piped input records.
What it assumes
Assumes familiarity with Kafka producers, consumers, Connect, and Streams, then introduces testing practices for their applications.
What you can do after
Apply it via MockConsumer, MockProducer, TopologyTestDriver, and Testcontainers to validate Kafka applications with or without a live broker.
How it connects
Connects unit testing and integration testing with mocks, Kafka producers, consumers, Kafka Streams, TopologyTestDriver, and Testcontainers.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.