Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kafka embedded doesn't start for unit tests #435

Closed
msillence opened this issue Nov 12, 2021 · 7 comments · Fixed by #777
Closed

kafka embedded doesn't start for unit tests #435

msillence opened this issue Nov 12, 2021 · 7 comments · Fixed by #777
Assignees
Labels
info: good first issue Good for newcomers status: pr submitted A pull request has been submitted for the issue type: docs

Comments

@msillence
Copy link
Contributor

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 graemerocher transferred this issue from micronaut-projects/micronaut-core Nov 12, 2021
@graemerocher
Copy link
Contributor

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
Copy link
Contributor

@msillence
Copy link
Contributor Author

msillence commented Nov 12, 2021

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
Copy link
Contributor

graemerocher commented Nov 12, 2021

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
Copy link
Contributor Author

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
Copy link
Contributor

leaving it here as a reminder :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info: good first issue Good for newcomers status: pr submitted A pull request has been submitted for the issue type: docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants