2V0-72.22PSE · Question #75
Refer to the exhibit. What statement is true about @DirtiesContext?
The correct answer is B. It will close the existing cached ApplicationContext and recreate a new one after the test method. @DirtiesContext defaults to AFTER_METHOD mode, meaning it marks the Spring ApplicationContext as dirty and triggers it to be closed and rebuilt after the annotated test method finishes - making B correct. Why the distractors are wrong: A describes @DirtiesContext(methodMode =…
Question
Refer to the exhibit. What statement is true about @DirtiesContext?
Exhibit
Options
- AIt will close the existing cached ApplicationContext and recreate a new one before the test
- BIt will close the existing cached ApplicationContext and recreate a new one after the test method.
- CIt will keep the existing cached ApplicationContext, all changes to Spring managed beans will be
- DIt will recreate only the Spring managed beans that are modified inside the test method.
How the community answered
(25 responses)- A4% (1)
- B88% (22)
- C8% (2)
Explanation
@DirtiesContext defaults to AFTER_METHOD mode, meaning it marks the Spring ApplicationContext as dirty and triggers it to be closed and rebuilt after the annotated test method finishes - making B correct.
Why the distractors are wrong:
- A describes
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD), which is a valid but non-default configuration - the default is after, not before. - C is the opposite of what the annotation does; its entire purpose is to invalidate the cached context, not preserve it with in-place changes.
- D is incorrect because
@DirtiesContextrebuilds the entireApplicationContext, not just the specific beans that were modified - Spring has no mechanism to selectively recreate individual beans on that basis.
Memory tip: Read the name literally - "dirties" means you've contaminated the context (e.g., modified a bean's state), so Spring tears it down after you're done with it, like washing a dish after a meal, not before.
Topics
Community Discussion
No community discussion yet for this question.
