Reputation: 241
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.
Upvotes: 3
Views: 6828
Reputation: 3908
Here are the settings that worled for me:
...
<properties>
<java.version>1.8</java.version>
<testcontainers.version>1.14.3</testcontainers.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
<lettuce.version>5.3.3.RELEASE</lettuce.version>
<lombok.version>1.18.12</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-messaging</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>${lettuce.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JUnit 5 dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- Testcontainers dependencies -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Test class exmaple:
@SpringBootTest
@Testcontainers
class KafkaProducerTest {
@Container
public KafkaContainer container = new KafkaContainer();
@Test
void sendMessage() {
assertTrue(container.isRunning());
}
}
Upvotes: 2
Reputation: 974
You are missing the following dependency:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.13.0</version>
<scope>test</scope>
</dependency>
Upvotes: 1
Reputation: 155
There are certainly more sophisticated ways to do this, but I think you just need to:
@Override
public void beforeAll(ExtensionContext context) throws Exception {
while(!this.kafkaContainer.isRunning());
logger.info("start Kafka docker!!");
}
If that works, you should add a real async framework to the mix and implement more mature retries and timeouts.
Upvotes: 0