Created
November 11, 2021 17:13
-
-
Save schroedermatt/c304ddbea96638fbb0ee852145b4efa5 to your computer and use it in GitHub Desktop.
EmbeddedKafkaTestResource for Quarkus tests
This file contains hidden or 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
import org.springframework.kafka.test.EmbeddedKafkaBroker; | |
public class EmbeddedKafkaTestResource implements QuarkusTestResourceLifecycleManager { | |
public EmbeddedKafkaBroker embeddedBroker; | |
/** | |
* @return A map of system properties that should be set for the running test | |
*/ | |
@Override | |
public Map<String, String> start() { | |
embeddedBroker = new EmbeddedKafkaBroker(1); | |
Map<String,String> brokerProperties = new HashMap<>(); | |
// add any specific broker properties here (auto topic creation, default topic settings, etc) | |
// brokerProperties.put("key","value"); | |
embeddedBroker.brokerProperties(brokerProperties); | |
// The system property with this name is set to the list of broker addresses | |
embeddedBroker.brokerListProperty("kafka.bootstrap.servers"); | |
// initialize the broker | |
embeddedBroker.afterPropertiesSet(); | |
// map of system properties that should be set for the running test | |
Map<String, String> props = new HashMap<>(); | |
props.put("kafka.bootstrap.servers", embeddedBroker.getBrokersAsString()); | |
props.put("quarkus.kafka-streams.bootstrap-servers", embeddedBroker.getBrokersAsString()); | |
return props; | |
} | |
@Override | |
public void stop() { | |
if (embeddedKafkaBroker != null) { | |
embeddedKafkaBroker.destroy(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment