I'm trying to write an integration test for my Kafka consumer.
I'm using JUnit 5
, so I can׳t initialize it using @Rule
, and the examples I saw with @Container
initialization it is not working as well.
I tried to change my Junit version to Junit 4
but it damages my other tests (so I need to stay with Junit 5
).
I tried to use this example at Junit 4: https://www.testcontainers.org/modules/kafka/
And those on Junit 5
: https://www.hascode.com/2019/01/using-throwaway-containers-for-integration-testing-with-java-junit-5-and-testcontainers/
But it doesn't recognize my annotations (@Testcontainers
, @Container
).
Gradle imports:
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.4.0'
implementation group: 'org.apache.kafka', name: 'kafka-clients', version: '1.1.1'
testIntegrationImplementation "org.testcontainers:kafka:1.11.4"
I'm uploading this code as an annotation:
@Testcontainers
public class KafkaTestContainer implements BeforeAllCallback, AfterAllCallback {
@Container
public KafkaContainer kafkaContainer = new KafkaContainer();
private static final Logger logger = LoggerFactory.getLogger(KafkaTestContainer.class);
@Inject
private KafkaTestContainer() {
try {
} catch (Exception e) {
logger.error(e.getMessage());
}
}
private String getKafkaBootstrapServers(Request request) throws IOException {
return this.kafkaContainer.getBootstrapServers();
}
public void stopKafkaTestContainer() {
// Stop the container.
kafkaContainer.stop();
}
@Override
public void afterAll(ExtensionContext context) throws Exception {
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
boolean isKafkaRunning = this.kafkaContainer.isRunning();
if(isKafkaRunning) {
logger.info("start Kafka docker!!");
}
}
isKafkaRunning value is always false.
- Any help with Kafka test container initialization is appreciated?
- What am I missing??