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?