BaseExclusiveKafkaTestSupport.java

Overview

`BaseExclusiveKafkaTestSupport` is an abstract base class designed to facilitate integration testing of Apache Camel routes that interact with Apache Kafka. It provides a reusable testing framework that manages the lifecycle and configuration of embedded Kafka services and Camel contexts, ensuring exclusive, isolated testing environments for Kafka-based integration tests.

This class streamlines the setup of Kafka infrastructure and Camel routes, allowing subclasses to focus solely on defining their specific Kafka-related routes while leveraging standardized Kafka service initialization, administration, and configuration procedures.


Detailed Description

Package

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

Purpose


Class: BaseExclusiveKafkaTestSupport

An abstract class implementing the `ConfigurableRoute` interface to standardize Kafka test setup.

Fields

Field Name

Type

Description

`service`

`KafkaService`

A Kafka service instance created via `KafkaServiceFactory`. It manages Kafka broker lifecycle for tests.

`contextExtension`

`CamelContextExtension`

Manages the lifecycle of Camel contexts used in tests.

`kafkaAdminClient`

`AdminClient`

Kafka Admin client used for Kafka administrative operations such as topic management.

Annotations


Methods

void beforeClass()

void setKafkaAdminClient()

void configureKafka(CamelContext context)

void createRouteBuilder(CamelContext context) throws Exception

abstract RouteBuilder createRouteBuilder()

Properties getDefaultProperties()

static String getBootstrapServers()


Implementation Details


Integration with Other System Components


Usage Example

public class MyKafkaRouteTest extends BaseExclusiveKafkaTestSupport {

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

    @Test
    public void testKafkaRoute() throws Exception {
        // Use CamelTemplate to send/receive messages and assert results
    }
}

Mermaid Class Diagram

classDiagram
    class BaseExclusiveKafkaTestSupport {
        <<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()
    }

    class KafkaService
    class CamelContextExtension
    class AdminClient
    class CamelContext
    class RouteBuilder
    class Properties

    BaseExclusiveKafkaTestSupport ..> KafkaService : uses
    BaseExclusiveKafkaTestSupport ..> CamelContextExtension : uses
    BaseExclusiveKafkaTestSupport ..> AdminClient : uses
    BaseExclusiveKafkaTestSupport ..> CamelContext : parameter
    BaseExclusiveKafkaTestSupport ..> RouteBuilder : returns/abstract
    BaseExclusiveKafkaTestSupport ..> Properties : returns

Summary

`BaseExclusiveKafkaTestSupport.java` is a foundational test support class for Apache Camel Kafka integration tests. It abstracts Kafka service instantiation, Camel context configuration, and Kafka admin client management, facilitating reusable, isolated, and consistent Kafka test environments. Subclasses extend this base class to define specific Kafka routes and test logic, benefiting from the robust infrastructure and lifecycle management provided.

This design promotes clean separation of concerns, reusability, and ease of writing Kafka integration tests within the Apache Camel ecosystem.