Kafka
Gradle
GitHub Actions
Integration Testing
Continuous Integration

Kafka integration tests in Gradle runs into GitHub Actions

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Integrating Apache Kafka tests into Gradle and subsequently executing them on GitHub Actions poses a powerful way to continuously ensure the reliability and integrity of messaging functionalities in applications. This article dives deep into how to facilitate such integration effectively.

Apache Kafka and Gradle Setup

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. To test Kafka interactions within Java applications, Kafka provides an embedded mode which can be very handy for integration testing.

Using Gradle, a modern open-source build automation system, you can setup your project to automatically manage dependencies and configure tasks to run tests, including those for Kafka. Below is an example of how to configure a Gradle project to use Kafka:

groovy
1plugins {
2    id 'java'
3}
4
5group 'com.example'
6version '1.0-SNAPSHOT'
7
8repositories {
9    mavenCentral()
10}
11
12dependencies {
13    testImplementation 'org.apache.kafka:kafka-clients:2.8.0'
14    testImplementation 'org.springframework.kafka:spring-kafka-test:2.7.2'
15}
16
17test {
18    useJUnitPlatform()
19}

Writing Kafka Integration Tests

Integration tests for Kafka usually involve setting up a Kafka producer to send messages and a consumer to receive them. The spring-kafka-test provides EmbeddedKafkaBroker that can be used for creating an in-memory Kafka instance exclusively for testing purposes:

java
1@SpringBootTest
2public class KafkaProducerConsumerIT {
3
4    private static final String TOPIC = "test-topic";
5
6    @Autowired
7    private KafkaProducer<String, String> producer;
8
9    @Autowired
10    private KafkaConsumer<String, String> consumer;
11
12    @Test
13    public void testSendMessage() {
14        producer.send(new ProducerRecord<>(TOPIC, "key", "Hello Kafka"));
15        ConsumerRecord<String, String> record = KafkaTestUtils.getSingleRecord(consumer, TOPIC);
16        assertEquals("Hello Kafka", record.value());
17    }
18}

Setting Up GitHub Actions

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Here’s how you can set up GitHub Actions to run your Gradle-based Kafka integration tests:

  1. Create a YAML file for your GitHub Actions workflow: Under your repository, create a directory named .github/workflows and add a YAML file, e.g., ci.yml.
  2. Define Workflow: In the ci.yml file, define steps that setup Java, checkout the code, setup Gradle, and run the tests.
yaml
1name: Java CI
2
3on:
4  push:
5    branches: [ main ]
6  pull_request:
7    branches: [ main ]
8
9jobs:
10  build:
11    runs-on: ubuntu-latest
12
13    steps:
14    - uses: actions/checkout@v2
15    - name: Set up JDK 11
16      uses: actions/setup-java@v2
17      with:
18        java-version: '11'
19        distribution: 'adopt'
20
21    - name: Grant execute permission for gradlew
22      run: chmod +x gradlew
23
24    - name: Build with Gradle
25      run: ./gradlew build
26
27    - name: Run integration tests
28      run: ./gradlew test --tests *IT

Summary Table

FeatureTool/FrameworkDescription
Build automationGradleHandles project dependencies and tasks like running tests.
Testing frameworkJUnit, spring-kafka-testFacilitates writing and executing tests including embedded Kafka.
CI/CDGitHub ActionsAutomates the process of running tests upon code pushes or PRs.
Kafka ImplementationEmbedded Kafka via spring-kafka-testProvides a Kafka Broker environment specifically for testing.

Additional Considerations

  • Security and Kafka: When setting up Kafka in production scenarios, consider securing the brokers using SSL and SASL.
  • Kafka Cluster: For more realistic integration testing, consider setting up a multi-broker Kafka cluster if resources permit.
  • Monitoring and Logging: Integrate monitoring tools like Prometheus and logging tools like ELK stack for better observation of the Kafka environment.

Conclusion

The combination of Kafka integration tests with Gradle for build management and GitHub Actions for CI/CD provides a robust framework for developing high-quality, reliable Kafka-based applications. This integration empowers developers to continuously test and improve the codebase with minimal manual intervention.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.