BaseKafkaTestSupport.java


Overview

`BaseKafkaTestSupport.java` is an abstract base class designed to simplify the creation and management of integration tests involving Apache Kafka within the Apache Camel framework. It provides foundational support for setting up Kafka services, configuring the Camel context with Kafka components, and managing Kafka administrative clients. By extending this class, developers can focus on defining Kafka routes in their tests without worrying about the underlying Kafka infrastructure setup and configuration details.

Key functionalities provided by this class include:

This class integrates tightly with Camel's testing infrastructure and Kafka testing utilities to streamline Kafka-related integration testing.


Class: BaseKafkaTestSupport

Package

`org.apache.camel.component.kafka.integration`

Inheritance

Description

An abstract test support class to facilitate Kafka integration testing using Apache Camel. It manages Kafka service lifecycle and Camel context configuration while enforcing the implementation of Kafka routes via the `createRouteBuilder()` method.


Fields

Modifier

Type

Name

Description

`protected static`

`KafkaService`

`service`

Singleton Kafka service instance used for testing.

`protected static`

`CamelContextExtension`

`contextExtension`

Extension to manage CamelContext lifecycle in tests.

`protected static`

`AdminClient`

`kafkaAdminClient`

Kafka AdminClient instance for administrative operations.


Methods

void beforeClass()


void setKafkaAdminClient()


void configureKafka(CamelContext context)


void createRouteBuilder(CamelContext context) throws Exception


abstract RouteBuilder createRouteBuilder()

@Override
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("kafka:my-topic?brokers=" + getBootstrapServers())
                .to("mock:result");
        }
    };
}

Properties getDefaultProperties()


static String getBootstrapServers()


Implementation Details and Algorithms


Interaction with Other System Components

This class acts as a bridge between Kafka test services and Camel's routing engine, providing a reusable and configurable foundation for Kafka integration tests within Camel components.


Usage Example

public class MyKafkaIntegrationTest extends BaseKafkaTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("kafka:input-topic?brokers=" + getBootstrapServers())
                    .to("log:received-message")
                    .to("mock:result");
            }
        };
    }

    @Test
    public void testKafkaRoute() {
        // Test logic using Kafka producer and consumer with configured route
    }
}

Mermaid Class Diagram

classDiagram
    class BaseKafkaTestSupport {
        <<abstract>>
        - static KafkaService service
        - static CamelContextExtension contextExtension
        - static AdminClient kafkaAdminClient
        + void beforeClass()
        + void setKafkaAdminClient()
        + void configureKafka(CamelContext context)
        + void createRouteBuilder(CamelContext context) throws Exception
        + abstract RouteBuilder createRouteBuilder()
        + Properties getDefaultProperties()
        + static String getBootstrapServers()
    }
    BaseKafkaTestSupport ..|> ConfigurableRoute

Summary

`BaseKafkaTestSupport.java` is a foundational abstract class designed to streamline Kafka integration testing with Apache Camel. It manages Kafka service lifecycle, Camel context Kafka configuration, and Kafka administrative client initialization. By enforcing route definition through an abstract method, it promotes clean separation of test infrastructure and test logic. This class is an essential piece for developers aiming to write concise, maintainable, and effective Kafka-related integration tests within the Apache Camel ecosystem.