title: Spring Boot test: @Scheduled beans continue to execute after dirty context
body: <p><strong>Context</strong></p>
  <p>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.</p>
  <p>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.</p>
  <p><strong>Problem</strong></p>
  <p>This works fine until we write a test that <strong>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.</strong></p>
  <p>I've demonstrated this problem in this little example: <a href="https://github.com/stainii/spring-boot-test-scheduled-bean-problem" rel="nofollow noreferrer">https://github.com/stainii/spring-boot-test-scheduled-bean-problem</a>. 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.</p>
  <p>Does anyone know if this is the expected behaviour and if we could destroy the @Scheduled bean when the context is marked as dirty?</p>