liveBook · Manning

@dirtiescontext

@DirtiesContext is a Spring testing annotation that marks an application context for removal from the test context cache and subsequent recreation.

How 2 Manning books explain it

Java Persistence with Spring Data and Hibernate epub

@DirtiesContext is a Spring testing annotation that marks an application context as modified so it can be discarded and recreated for subsequent tests.

How it is used
Isolates Spring persistence tests when a method or class modifies shared application-context or database state.
How deep it goes
Explains method and class scope plus four lifecycle timings, but defers implementation internals beyond context recreation and caching.
Examples
Uses a repeated SaveRetrieveUserTest, recreated tables, inserted users, Spring Boot startup banners, and transaction-isolation comparisons.
What you can do after
Operate/configure Spring tests with scoped context recreation and recognize its isolation and execution-time tradeoff.

Read how Java Persistence with Spring Data and Hibernate epub explains @dirtiescontext →

Testing Spring Boot Applications

@DirtiesContext is a Spring testing annotation that marks an application context as dirty so it is removed from the test context cache and recreated when needed.

How it is used
Evicts a reusable Spring test application context after destructive tests so subsequent tests receive a recreated context.
How deep it goes
Explains method and class scope, recreation timing, external-state limits, and performance tradeoffs without detailing annotation internals.
Examples
Uses a destructive breakStuff test annotated to recreate the test application after completion.
What you can do after
Apply @DirtiesContext to destructive tests and combine it with manual cleanup for state outside the application context.

Read how Testing Spring Boot Applications explains @dirtiescontext →

Related concepts

Questions about @dirtiescontext

What is @dirtiescontext?

@DirtiesContext is a Spring testing annotation that marks an application context for removal from the test context cache and subsequent recreation.

Which Manning books explain @dirtiescontext?

Java Persistence with Spring Data and Hibernate epub and Testing Spring Boot Applications.

What is @dirtiescontext a kind of?

@dirtiescontext is a kind of attribute. A programming-language attribute is declarative metadata associated with a program element that provides information or directives to compilers, tools, frameworks, or runtimes.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.