micronaut-projects / micronaut-kafka

Integration between Micronaut and Apache Kafka
Apache License 2.0
83 stars 104 forks source link

kafka embedded doesn't start for unit tests #435

Closed msillence closed 1 year ago

msillence commented 2 years ago

Expected Behavior

adding the property kafka.embedded.enabled=true should start kafka so unit tests work

Actual Behaviour

unit tests fail trying to connect to kafka

Steps To Reproduce

From the documentation https://micronaut-projects.github.io/micronaut-kafka/latest/guide/#kafkaEmbedded

I believe that kafka.embedded.enabled=true should start kafka embedded for the unit tests

I find the test fail looping trying to connect, however if I add the annoation @Requires(property = "foo.bar", value = "stuff") then the tests all pass ( the don't use or need that property ) I added this as part of following the how to add per class lifecyle listeners ( I was going to try and spin up a kafka test container ) I'd much prefer not to spin up a container as it is much quicker using the embedded kafka

I feel this annotation is obviously a side effect and would like to know the correct way to spin up the embeded kafka

I've also tried the annotation from https://blog.wick.technology/micronaut-testing-kafka/ adding @MicronautTest(environments = "kafka") but that makes no difference

Environment Information

ubuntu linux (WSL2) / windows java 17

Example Application

https://github.com/msillence/micronaut-kafka-unit-test

Version

3.1.3

graemerocher commented 2 years ago

Support for embedding kafka was removed in version 4.0.0 in favour of test containers, the documentation should have been corrected for this though

graemerocher commented 2 years ago

See for example https://guides.micronaut.io/latest/micronaut-kafka-gradle-java.html

msillence commented 2 years ago

Thanks for the quick reply, is it possible to start up the kafka container once for all the tests? Is there some overall test lifecycle listener option? It seems when I include the postgres test container that automatically starts up without any codea and runs for all the tests? - actually that's not right postgres seems to start up for each test class as well.

graemerocher commented 2 years ago

Sure https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers

graemerocher commented 2 years ago

A more complete example for JUnit 5 would look something like:

abstract class AbstractKafkaTest implements TestPropertyProvider {

    static final KafkaContainer MY_KAFKA;

    static {
        MY_KAFKA = new KafkaContainer(
            DockerImageName.parse("confluentinc/cp-kafka:latest")); 
        MY_KAFKA.start();
    }

     @Override
    public Map<String, String> getProperties() {
        return Collections.singletonMap(
                "kafka.bootstrap.servers", MY_KAFKA.getBootstrapServers() 
        );
    }
}

And then test:

@MicronautTest
class MyTest extends AbstractKafkaTest {

}
msillence commented 2 years ago

that's fantastic, thank you. do you want to close this or should we keep this open as a reminder to update the docs?

graemerocher commented 2 years ago

leaving it here as a reminder :)