-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : using apachekafka container for tests (#527)
* 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
1 parent
b0f4cca
commit e8497f4
Showing
4 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 2 additions & 19 deletions
21
...ud-sample/src/test/java/com/example/cloudkafkasample/TestCloudKafkaSampleApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ring-cloud-sample/src/test/java/com/example/cloudkafkasample/common/ContainersConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |