spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

How to test events from other threads?

Open jekutzsche opened this issue 8 months ago • 10 comments
trafficstars

Hi, I am testing the response to events using org.springframework.modulith.test.Scenario. The test publishes an event, my code processes it and should again publish events that the test checks for. The processing and publishing of the new events should be done asynchronously using JobRunr.

If events are now published via the ApplicationEventPublisher within a separate thread (the JobRunr job), then Scenario.andWaitForEventOfType does not react to this in the tests.

If I have found this out correctly, it is because the ThreadBoundApplicationListenerAdapter used by the PublishedEventsParameterResolver has not registered a listener for the JobRunr thread. A listener is only registered for the main thread in the test. I now assume that it works with the threads of @Async (the test works there without any problems, although it is also a different thread) because they inherit from Main. However, this does not seem to be the case with JobRunr threads.

From the comments on ThreadBoundApplicationListenerAdapter, I assume that this behavior is intended. However, I have not found a way to wait correctly for the events from the JobRunr threads. What is the concept behind this and what is the best way to implement the tests?

jekutzsche avatar Feb 22 '25 15:02 jekutzsche