1. Build Applications
  2. Kafka Clients

Test Ursa as code

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

  • Local testing: Ursa testcontainer module allows you to start a Ursa service with one line code. This will facilitate the convenience of conducting tests and experiencing it locally at any time.
  • Integration Testing: Ursa testcontainer module is particularly useful for integration testing, where you need to test the interactions between your application and Ursa dependencies. Instead of relying on mocked or stubbed versions of these dependencies, Ursa 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, Ursa testcontainer module can help ensure consistent behavior across these environments. By encapsulating your Ursa dependency in containers, you can run your tests on any machine that supports Docker, regardless of the underlying operating system or infrastructure.
  • Dependency Management: Ursa 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: Ursa 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): Ursa 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 Ursa testcontainer module in your CI/CD pipeline, you can ensure that your application's integration points are thoroughly tested before deployment.

Step 1: Import Ursa testcontainer

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

<dependency>
  <groupId>org.testcontainers</groupId>
  <artifactId>testcontainers</artifactId>
  <version>1.19.1</version>
</dependency>
<dependency>
  <groupId>io.streamnative.ksn</groupId>
  <artifactId>ksn-testcontainer</artifactId>
  <version>0.1.0</version>
</dependency>

Step 2: Set up a Ursa cluster

final KsnCluster cluster = new KsnCluster();

Step 3: Use the created Ursa 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 Ursa cluster after testing

cluster.close();
Previous
Migrating to StreamNative