Solving the hanging tests in Micronaut application

We've been facing a strange issue with our Micronaut application tests. Specifically tests that were producing Kafka messages were successfully run bug sometimes the test engine was kept hanging and never finishing.


We are running the following versions:

This problem seems related with this issue described here:

After some investigation we've found out that the issue was caused by the fact that sometimes some KafkaListener in our projects were still active and processing messages even after the test was finished. This caused the application to keep running and the test engine to wait for the application to finish.

Our solution was to add a @PreDestroy method to a new AppApplicationListener singleton so that the listeners can terminate their process so that the application shutdown can be successful.

public class AppEventListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppEventListener.class);

    public void onStartupEvent(StartupEvent event) {

    void onShutdown() throws InterruptedException {"PreDestroy: Shutting down after a sleep of 1000ms to allow the tests to complete.");
        LOGGER.debug("It looks like the test is stuck if we don't implement this @AfterAll method. " +
                "This is an issue with Micronaut and JUnit 5." +

← Back to home