From a51e034186bc8f730dbfbe9cc2230ccf7a47606a Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Fri, 13 Nov 2020 16:42:33 +0100 Subject: [PATCH] BAEL-4609 - Testing Kafka and Spring Boot --- spring-kafka/pom.xml | 7 +++++-- .../EmbeddedKafkaIntegrationTest.java | 21 ++++++++----------- .../KafkaTestContainersIntegrationTest.java | 12 ----------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/spring-kafka/pom.xml b/spring-kafka/pom.xml index 3673fe8fa5..235dd75966 100644 --- a/spring-kafka/pom.xml +++ b/spring-kafka/pom.xml @@ -22,6 +22,7 @@ org.springframework.kafka spring-kafka + ${spring-kafka.version} com.fasterxml.jackson.core @@ -30,18 +31,20 @@ org.springframework.kafka spring-kafka-test + ${spring-kafka.version} test org.testcontainers kafka + ${testcontainers-kafka.version} test - 1.15.0-rc2 - 2.3.7.RELEASE + 2.5.8.RELEASE + 1.15.0 \ No newline at end of file diff --git a/spring-kafka/src/test/java/com/baeldung/kafka/embedded/EmbeddedKafkaIntegrationTest.java b/spring-kafka/src/test/java/com/baeldung/kafka/embedded/EmbeddedKafkaIntegrationTest.java index 49ea080369..4c727795c4 100644 --- a/spring-kafka/src/test/java/com/baeldung/kafka/embedded/EmbeddedKafkaIntegrationTest.java +++ b/spring-kafka/src/test/java/com/baeldung/kafka/embedded/EmbeddedKafkaIntegrationTest.java @@ -1,22 +1,19 @@ package com.baeldung.kafka.embedded; -import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.kafka.test.context.EmbeddedKafka; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringRunner; - import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import java.util.concurrent.TimeUnit; -@RunWith(SpringRunner.class) +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.kafka.test.context.EmbeddedKafka; +import org.springframework.test.annotation.DirtiesContext; + @SpringBootTest @DirtiesContext @EmbeddedKafka(partitions = 1, brokerProperties = { "listeners=PLAINTEXT://localhost:9092", "port=9092" }) @@ -45,7 +42,7 @@ class EmbeddedKafkaIntegrationTest { @Test public void givenEmbeddedKafkaBroker_whenSendingtoSimpleProducer_thenMessageReceived() throws Exception { - producer.send(topic, "Sending with own controller"); + producer.send(topic, "Sending with our own simple KafkaProducer"); consumer.getLatch().await(10000, TimeUnit.MILLISECONDS); assertThat(consumer.getLatch().getCount(), equalTo(0L)); diff --git a/spring-kafka/src/test/java/com/baeldung/kafka/testcontainers/KafkaTestContainersIntegrationTest.java b/spring-kafka/src/test/java/com/baeldung/kafka/testcontainers/KafkaTestContainersIntegrationTest.java index cf941b770f..972b6cce4d 100644 --- a/spring-kafka/src/test/java/com/baeldung/kafka/testcontainers/KafkaTestContainersIntegrationTest.java +++ b/spring-kafka/src/test/java/com/baeldung/kafka/testcontainers/KafkaTestContainersIntegrationTest.java @@ -12,8 +12,6 @@ import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.serialization.StringDeserializer; import org.apache.kafka.common.serialization.StringSerializer; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; @@ -59,16 +57,6 @@ public class KafkaTestContainersIntegrationTest { @Value("${test.topic}") private String topic; - @BeforeClass - public static void setUpBeforeClass() throws Exception { - kafka.start(); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - kafka.stop(); - } - @Test public void givenKafkaDockerContainer_whenSendingtoDefaultTemplate_thenMessageReceived() throws Exception { template.send(topic, "Sending with default template");