Set up Kafka Streams testing framework with TopologyTestDriver for automated stream processing validation

Intermediate 45 min May 15, 2026 549 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Configure a complete testing framework for Kafka Streams applications using TopologyTestDriver to validate stream processing logic with automated tests and mock data pipelines.

Prerequisites

  • Java 11 or later
  • Maven or Gradle build tool
  • Basic understanding of Apache Kafka concepts

What this solves

Testing Kafka Streams applications in production environments is complex and expensive. The TopologyTestDriver provides a lightweight testing framework that simulates Kafka brokers and topics without requiring a full Kafka cluster. This enables fast, automated unit tests for stream processing logic, topology validation, and data transformation pipelines.

Step-by-step installation

Install Java Development Kit

Kafka Streams requires Java 11 or later for development and testing.

sudo apt update
sudo apt install -y openjdk-17-jdk maven gradle
sudo dnf update -y
sudo dnf install -y java-17-openjdk-devel maven gradle

Download and install Apache Kafka

Install Kafka with Scala 2.13 for Streams API compatibility.

cd /opt
sudo wget https://archive.apache.org/dist/kafka/2.8.2/kafka_2.13-2.8.2.tgz
sudo tar -xzf kafka_2.13-2.8.2.tgz
sudo mv kafka_2.13-2.8.2 kafka
sudo chown -R $(whoami):$(whoami) /opt/kafka

Set up environment variables

Configure Java and Kafka paths for development tools.

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export KAFKA_HOME=/opt/kafka
export PATH=$PATH:$KAFKA_HOME/bin:$JAVA_HOME/bin
source ~/.bashrc
java -version
echo $KAFKA_HOME

Create Maven project structure

Set up a Maven project with proper directory structure for Kafka Streams testing.

mkdir -p kafka-streams-testing
cd kafka-streams-testing
mvn archetype:generate -DgroupId=com.example.streams \
    -DartifactId=kafka-streams-test \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false

Configure Maven dependencies

Add Kafka Streams, TopologyTestDriver, and testing dependencies to your project.


Create Gradle build configuration

Alternative Gradle setup for projects preferring Gradle over Maven.

plugins {
    id 'java'
    id 'application'
}

group = 'com.example.streams'
version = '1.0.0'

java {
    sourceCompatibility = '17'
    targetCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.kafka:kafka-streams:2.8.2'
    implementation 'org.slf4j:slf4j-simple:1.7.36'
    
    testImplementation 'org.apache.kafka:kafka-streams-test-utils:2.8.2'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
        exceptionFormat "full"
    }
}

application {
    mainClass = 'com.example.streams.StreamProcessor'
}

Create stream processing application

Build a sample Kafka Streams application for testing word count processing.

package com.example.streams;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Produced;

import java.util.Arrays;
import java.util.Properties;

public class StreamProcessor {
    
    public static Topology createTopology() {
        StreamsBuilder builder = new StreamsBuilder();
        
        KStream

Implement TopologyTestDriver tests

Create comprehensive unit tests using TopologyTestDriver for stream processing validation.

package com.example.streams;

import org.apache.kafka.common.serialization.LongDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.test.TestRecord;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

class StreamProcessorTest {
    
    private TopologyTestDriver testDriver;
    private TestInputTopic

Create advanced testing scenarios

Implement tests for error handling and edge cases in stream processing.

package com.example.streams;

import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.test.TestRecord;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.Instant;

import static org.junit.jupiter.api.Assertions.*;

class AdvancedStreamTest {
    
    private TopologyTestDriver testDriver;
    private TestInputTopic

Configure test automation with Maven

Set up automated test execution and reporting with Maven surefire plugin.

cd kafka-streams-test
mvn clean compile
mvn test
# Run specific test class
mvn test -Dtest=StreamProcessorTest

# Run tests with detailed output
mvn test -Dtest=StreamProcessorTest -DforkCount=1 -DreuseForks=false

Configure test automation with Gradle

Alternative Gradle configuration for automated testing and continuous integration.

# Build and run all tests
./gradlew test

# Run specific test class
./gradlew test --tests StreamProcessorTest

# Generate test reports
./gradlew test jacocoTestReport
// Add to existing build.gradle
apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.7"
}

jacocoTestReport {
    reports {
        xml.required = false
        csv.required = false
        html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
    }
}

Set up continuous integration testing

Create GitHub Actions workflow for automated testing on code changes.

name: Kafka Streams Tests

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        java-version: [11, 17]
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Set up JDK ${{ matrix.java-version }}
      uses: actions/setup-java@v3
      with:
        java-version: ${{ matrix.java-version }}
        distribution: 'temurin'
    
    - name: Cache Maven dependencies
      uses: actions/cache@v3
      with:
        path: ~/.m2
        key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
    
    - name: Run tests
      run: mvn clean test
    
    - name: Generate test report
      run: mvn surefire-report:report
    
    - name: Upload test results
      uses: actions/upload-artifact@v3
      if: always()
      with:
        name: test-results-java-${{ matrix.java-version }}
        path: target/surefire-reports/

Verify your setup

# Check Java installation
java -version
javac -version

# Verify Maven build
mvn --version
mvn clean compile

# Run all tests
mvn test

# Check test results
ls -la target/surefire-reports/
cat target/surefire-reports/TEST-*.xml

Common issues

SymptomCauseFix
Tests fail with ClassNotFoundExceptionMissing kafka-streams-test-utils dependencyAdd test-utils dependency to pom.xml with test scope
TopologyTestDriver doesn't startInvalid Streams configurationEnsure APPLICATION_ID_CONFIG is set in test properties
Input/Output topics not foundTopic names don't match topologyVerify topic names in createInputTopic match stream builder
Serialization errors in testsMismatched serializers/deserializersUse consistent Serde types for test topics and topology
Maven build failsJava version incompatibilityEnsure Java 11+ and matching maven.compiler properties

Next steps

Running this in production?

Want this handled for you? Setting up Kafka Streams testing once is straightforward. Keeping it integrated with CI/CD, maintaining test environments, and ensuring comprehensive coverage across environments is the harder part. See how we run infrastructure like this for European teams building real-time data platforms.

Automated install script

Run this to automate the entire setup

Nie chcesz zarządzać tym samodzielnie?

Zarządzamy infrastrukturą firm, które zależą od dostępności. W pełni zarządzana, z jednym stałym kontaktem, który zna Twoje środowisko.

Macie jednego stałego opiekuna, który zna Waszą konfigurację

Rotterdam 03:11 · dostępny w wiadomości, bez formularza zgłoszeń