5

Context

I'm working on a Spring Boot project that makes heavy use of a @Scheduled bean. We have written a very light event-sourcing-like framework that relies on the data in the database: every x time the database is checked for new records, which can result in firing handlers for these new records.

We write integration tests: create a new record in the database, wait for the handler to pick it up and the database gets updated and verify the new state of the database.

Problem

This works fine until we write a test that dirties the spring context. The context gets recreated fine, but the @Scheduled method from the old context keeps running, executing logic from the old context instead of the new one.

I've demonstrated this problem in this little example: https://github.com/stainii/spring-boot-test-scheduled-bean-problem. When you run the tests (preferrably in alphabetical order for demonstration purposes), you'll see that each tests runs fine on its own, but when the context needs to get recreated then 2 scheduled beans are running instead of 1.

Does anyone know if this is the expected behaviour and if we could destroy the @Scheduled bean when the context is marked as dirty?

Stijn Hooft
  • 170
  • 1
  • 12
  • 2
    A and C reuse the same application context B creates a new context due to the `@TestPropertySource`. Net end result 2 times the scheduled task. With your current setup yes that is to be expected and works as designed, maybe not as expected. So it doesn't restart the application it starts a new one to be exactly (Spring Test caches 32 application contexts by default thus leaves it running). – M. Deinum Nov 08 '21 at 11:23
  • so, any solution to this? – René Winkler Jan 20 '22 at 09:06

0 Answers0