liveBook · Manning

Integration test

As explained in Kafka for Architects

Integration testing is the practice of verifying that multiple software components communicate and interoperate correctly through their defined contracts and protocols.

A service can pass a unit test and still fail to exchange messages through Kafka. An integration test crosses that boundary: it checks that components communicate through the agreed contract and protocol, rather than only checking how they behave in isolation. section 10.3 section 10.3.1

For Kafka clients, the distinction is practical. A unit test can use a mock to check that a service calls its producer or handles a supplied message. An integration test involves Kafka to verify that the producer publishes, the cluster retains the message, and the consumer retrieves and processes it. Load and throughput belong to performance testing, not this check. section 10.3.1 section 10.3.2 section 10.3

Test each side of the message flow from outside its Kafka boundary:

  • Producer: invoke the service, consume its message from the topic with a test consumer, and check the content and relevant metadata, such as headers, partition, or offset. section 10.3.2
  • Consumer: publish with a test producer, then check what the consumer passed to service logic or what effects processing produced. Mockito’s ArgumentCaptor can inspect the arguments passed to a service method. section 10.3.2

Choosing a broker for the test

An embedded Kafka broker lets a producer or consumer test exchange messages without an external cluster. Testcontainers instead launches a real Kafka broker in a Docker container for the test. A dedicated cluster is possible, but adds infrastructure overhead. The choice is how to run the Kafka interaction reliably, not whether to replace it with a mock. section 10.3.2 section 10.3.1

Figure 10.11 Integration testing of ProfileService using a Kafka cluster launched via Testcontainers. The test code starts a real Kafka broker inside a Docker container, invokes the service, and validates that the message was correctly sent to the Kafka topic.
A diagram of a software company

AI-generated content may be incorrect.

The diagram shows a service publishing to a Kafka broker launched by Testcontainers, with test code checking the message in the topic. section 10.3.2

How Kafka for Architects covers it

How deep it goes
Works through producer and consumer verification and broker setup options, but does not provide a code-level test walkthrough.
Examples
Uses service-to-Kafka producer and consumer tests, an embedded broker, and a Testcontainers-launched broker.
What it assumes
Assumes familiarity with services, Kafka producers, consumers, and topics; distinguishes integration tests from mock-based unit tests.
What you can do after
Practice/perform producer and consumer integration tests that verify messages or processing through Kafka rather than mocks alone.
How it connects
Connects unit testing, Kafka producers, Kafka consumers, message metadata, and performance testing while keeping their verification goals distinct.

Also explained in

Compare how 31 books explain integration test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka for Architects. The section links point to the parts of the book it draws on.