Search code examples
javaspringjunit

What does it exactly mean to use @DirtiesContext annotation on a class?


I have been working with Spring and I came across the @DirtiesContext annotation provided by spring. Using it on every method makes sense when you want to refresh the application context before running each method(of course at the expense of performance) but what does it exactly mean to annotate a test class with @DirtiesContext? An example would be really helpful.


Solution

  • If you annotate a test class with @DirtiesContext, it will remove the application context after the tests are run. There is a lot more explaining in this article: DirtiesContext annotation