Skip to content

Commit

Permalink
feat : using apachekafka container for tests (#527)
Browse files Browse the repository at this point in the history
* feat : using apachekafka container for tests

* fix : spotless issue

* Update TestCloudKafkaSampleApplication.java

* Update pom.xml

* fix : issues with dynamic properties

* revert changes
  • Loading branch information
rajadilipkolli authored Oct 29, 2024
1 parent b0f4cca commit e8497f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
6 changes: 3 additions & 3 deletions kafka-spring-cloud-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<version>3.4.0-RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
Expand All @@ -16,7 +16,7 @@

<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
<spring-cloud.version>2024.0.0-M2</spring-cloud.version>
<springdoc-openapi.version>2.6.0</springdoc-openapi.version>
<spotless.version>2.43.0</spotless.version>
</properties>
Expand Down Expand Up @@ -88,7 +88,7 @@
<configuration>
<java>
<palantirJavaFormat>
<version>2.38.0</version>
<version>2.50.0</version>
</palantirJavaFormat>
<importOrder/>
<removeUnusedImports/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

import com.example.cloudkafkasample.common.ContainersConfig;
import com.example.cloudkafkasample.sink.Receiver;
import java.time.Duration;
import org.junit.jupiter.api.Test;
Expand All @@ -15,7 +16,7 @@
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest(classes = TestCloudKafkaSampleApplication.class)
@SpringBootTest(classes = ContainersConfig.class)
@AutoConfigureMockMvc
class CloudKafkaSampleApplicationTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
/* (C)2023 */
package com.example.cloudkafkasample;

import com.example.cloudkafkasample.common.ContainersConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
class TestCloudKafkaSampleApplication {

@Bean
@ServiceConnection
KafkaContainer kafkaContainer(DynamicPropertyRegistry dynamicPropertyRegistry) {
KafkaContainer kafkaContainer = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka").withTag("7.6.2"))
.withKraft()
.withReuse(true);
dynamicPropertyRegistry.add("spring.cloud.stream.kafka.binder.brokers", kafkaContainer::getBootstrapServers);
return kafkaContainer;
}

public static void main(String[] args) {
SpringApplication.from(CloudKafkaSampleApplication::main)
.with(TestCloudKafkaSampleApplication.class)
.with(ContainersConfig.class)
.run(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* (C)2024 */
package com.example.cloudkafkasample.common;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistrar;
import org.testcontainers.kafka.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {

@Bean
@ServiceConnection
KafkaContainer kafkaContainer() {
return new KafkaContainer(DockerImageName.parse("apache/kafka-native").withTag("3.8.1")).withReuse(true);
}

@Bean
public DynamicPropertyRegistrar kafkaProperties(KafkaContainer kafkaContainer) {
return (properties) -> {
// Connect our Spring application to our Testcontainers Kafka instance
properties.add("spring.cloud.stream.kafka.binder.brokers", kafkaContainer::getBootstrapServers);
};
}
}

0 comments on commit e8497f4

Please sign in to comment.