60 seconds
Quick start
You can usually move to a shared broker in a few minutes. Add the dependency, declare the topics your tests need, and replace @EmbeddedKafka with @EnableWiretapKafkaBroker.
Add the dependency
Add the library to your test scope. Your own BOM can still override Spring and Kafka versions in the usual way.
<dependency>
<groupId>io.typecheck</groupId>
<artifactId>wiretap-spring-boot</artifactId>
<version>5.2.3</version>
<scope>test</scope>
</dependency>Declare shared topics
In src/test/resources/application.properties, list the topics that should exist for the suite.
wiretap.test.topics=orders,payments,notifications
wiretap.test.partitions=2If the suite needs different partition counts per topic, point the library at a YAML or JSON topic file instead:
wiretap.test.topics-file=classpath:kafka-topics.ymltopics:
- name: "orders"
partitions: 4
- name: "payments"
partitions: 2If your tests use Confluent Avro or Protobuf serialisers, enable the mock Schema Registry as well:
wiretap.test.mock-schema-registry.enabled=trueThe library then sets spring.kafka.properties.schema.registry.url to mock://wiretap for you.
Annotate your test
Replace @EmbeddedKafka with @EnableWiretapKafkaBroker:
@SpringBootTest
@EnableWiretapKafkaBroker
class OrderIntegrationTest {
@Autowired
private KafkaTemplate<String, OrderEvent> kafkaTemplate;
@Test
void shouldPublishOrder() throws Exception {
var order = OrderEvent.create("Widget", 3, new BigDecimal("29.99"));
kafkaTemplate.send("orders", order.getId(), order).get();
}
}That is enough to get the shared broker running.
What happens at runtime
When the first @EnableWiretapKafkaBroker test starts, the library:
- starts a single embedded Kafka broker for the JVM
- injects the broker address into the Spring environment
- creates the topics declared in your test configuration or topic file
- registers the broker as a Spring bean
After each test method, it clears records from non-internal topics and removes consumer groups so the next test starts cleanly without recreating the broker or restarting the Spring context.
Add topics for a single class
If one test class needs extra topics, declare them on the annotation:
@EnableWiretapKafkaBroker(topics = {"special-topic"}, partitions = 3)
class SpecialTopicTest {
}Those topics are added on top of anything declared in application.properties.
Use it with @TestConfiguration
Existing @TestConfiguration classes and @Primary bean overrides continue to work. By default, the library only supplies the broker and the properties your Kafka beans depend on. If production-only authentication is the reason you have test Kafka beans, see Bean Overrides.
@SpringBootTest
@EnableWiretapKafkaBroker
@Import(MyKafkaTestConfig.class)
class AuthBypassTest {
@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;
}Run the suite
This project treats integration tests as the default verification path.
- classes ending in
ITrun through Maven Failsafe mvn verifyruns the integration suite- coverage reports are generated during
verify