A pattern for structuring JUnit tests using @Nested classes and @RegisterExtension to create declarative scenario trees with scoped fixture lifecycle management. Each nested class represents a 'Given' clause, and fixtures set up once per scope using ExtensionContext.Store.computeIfAbsent with 'this' as the key. This ensures

12m read time From codecentric.de
Post cover image
Table of contents
The Typical ProblemThe patternThe piece that makes it work: Store + computeIfAbsentFixtures as factoriesCompatibilityWhen to useWhen not to useSummary

Sort: