Test KSN as code

TestContainers provides a lightweight, disposable containers for integration testing purposes. KSN allows for the seamless initiation of a KSN service alongside TestContainers, simplifying the testing process. You can use the KSN testcontainer module for:

  • Local testing KSN testcontainer module allows you to start a KSN service with one line code. This will facilitate the convenience of conducting tests and experiencing it locally at any time.
  • Integration Testing: KSN testcontainer module is particularly useful for integration testing, where you need to test the interactions between your application and KSN dependencies. Instead of relying on mocked or stubbed versions of these dependencies, KSN testcontainer module allows you to spin up real, isolated containers for each test run. This ensures that your tests closely resemble the actual runtime environment and can catch issues that may not be apparent in unit tests.
  • Cross-Platform Testing: If your application needs to run on different platforms or environments, KSN testcontainer module can help ensure consistent behavior across these environments. By encapsulating your KSN dependency in containers, you can run your tests on any machine that supports Docker, regardless of the underlying operating system or infrastructure.
  • Dependency Management: KSN testcontainer module simplifies the management of dependencies for your tests. Instead of manually setting up and tearing down external resources, Testcontainers handles the lifecycle of the containers, automatically starting them before your tests and stopping them afterward. This saves you from the hassle of maintaining complex setup and teardown code.
  • Parallel Testing: KSN testcontainer module supports parallel test execution, allowing you to run multiple tests concurrently without conflicts. Each test can have its own isolated container, ensuring independence and avoiding interference between tests.
  • Continuous Integration/Continuous Delivery (CI/CD): KSN testcontainer module integrates well with CI/CD pipelines, enabling you to run your integration tests as part of your automated build and deployment process. By including KSN testcontainer module in your CI/CD pipeline, you can ensure that your application's integration points are thoroughly tested before deployment.

Step 1: Import KSN testcontainer

You can import the KSN testcontainer dependency as well as the testcontainers dependency by adding the followings to your pom.xml


Step 2: Set up a KSN cluster

final KsnCluster cluster = new KsnCluster();

Step 3: Use the created KSN cluster in your tests

// Create a Kafka admin
final Properties adminProps = new Properties();
adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.getBootstrapServers());
final AdminClient client = AdminClient.create(adminProps);

// Create a Kafka producer
final Properties producerProps = new Properties();
producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.getBootstrapServers());
producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
final KafkaProducer<String, String> producer = new KafkaProducer<>(producerProps);

// Create a Kafka consumer
final Properties consumerProps = new Properties();
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.getBootstrapServers());
consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "group");
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProps);

Step 4: Close the KSN cluster after testing

Migrating to KSN