Embedded Kafka for Spring Boot Testing without using Docker

Sometimes it is nice to set up an embedded Kafka broker for testing without the need to have Docker installed (e.g. for using testcontainers-lib). The following snippet shows, how to set up an embedded Kafka instance for testing for a Spring Boot project. Setup Using Maven, this is our Spring Boot project with dependencies needed: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>(1) <version>2.6.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hascode.tutorial</groupId> <artifactId>kafka-testing</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>17</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <kafka.version>3.1.0</kafka.version>(2) </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId>(3) </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka-test</artifactId>(4) <scope>test</scope> </dependency> <dependency>(5) <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <scope>test</scope> </dependency> [..] </project> ...

May 25, 2022 · 4 min · 839 words · Micha Kops

JUnit5 Java Maven Snippet

Goals Use JUnit Maven BOM for version alignment Add minimal dependencies for JUnit5 Java Projekt Setup Excerpt from the Maven project’s pom.xml: <dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.7.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> </dependencies> JUnit5 does not work with older versions of the Maven Compiler Plugin and the Surefire Plugin used for test execution. Setting their versions in the pom.xml is done like this: ...

May 14, 2021 · 1 min · 85 words · Micha Kops

Using Throwaway Containers for Integration Testing with Java, JUnit 5 and Testcontainers.

A lot of boilerplate code is written when developers need to test their applications with different connected systems like databases, stream platforms and other collaborators. Docker allows to handle those dependencies but there is still some glue code required to bind the container’s lifecycle and the configuration to the concrete integration test. Testcontainers is a testing library that offers lightweight throwaway instances of anything able to run in a Docker container, with bindings to configure the specific containers and also provides wrappers to manage our own custom containers. ...

January 30, 2019 · 6 min · 1110 words · Micha Kops

Using JUnit 5 Parameterized Tests, Argument Sources and Converters

With JUnit 5 the possibilities to write parameterized tests have changed and improved a lot. The following short overview covers all new types of possible parameter sources for JUnit 5 tests as well as the new conversion API for test arguments. In addition we’re showing how parameterized tests were written in JUnit 4. Figure 1. Running JUnit5 in IntelliJ About We will be covering all available types of parameter sources in the following sections – all that you need as a prerequisite is Java ™, Maven and a few minutes of your time. ...

August 19, 2017 · 9 min · 1781 words · Micha Kops

IntelliJ Snippets

Live Templates JUnit 5 Test @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("$NAME$") void $METHOD$() throws Exception { $END$ } Figure 1. IntelliJ Live Template Editor Edit Variables: NAME METHOD: default-value camelCase(NAME) Figure 2. Editing template variables SLF4J Logger Template private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger( $CLASS$.class ); Edit variables: CLASS: expression: className() Favorite Shortcuts Select whole block in Editor Cmd+Tab+Down Select similar blocks in editor Ctrl+G Copy the absolute Path of the current File into Clipboard Cmd+Shift+C ...

March 1, 2010 · 2 min · 341 words · Micha Kops

Java Snippets

Remote Debug a Pod’s Java Process Simple steps for remote debugging a Java process running on a k8 pod: Edit deployment and add the following parameters to the Java start line: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=127.0.0.1:5005 Also add the following port mapping at the section container → ports in the deployment: - containerPort: 5005 protocol: TCP Safe, wait for the new pods and then add a port forward for port 5005 for this pod: kubectl port-forward podname 5005 ...

March 1, 2010 · 13 min · 2583 words · Micha Kops