
JUnit TempDir for Temporary Files and Test Resources
Create and clean up temporary files in JUnit with TempDir, share a directory per class, load files from test resources and avoid fragile paths in tests.
Tag
18 posts

Create and clean up temporary files in JUnit with TempDir, share a directory per class, load files from test resources and avoid fragile paths in tests.

Fix Mockito's UnnecessaryStubbingException by removing dead stubs, moving setup into the tests that use it and using lenient stubs only where they belong.

Feed multi parameter tests in JUnit with MethodSource, CsvSource, CsvFileSource and ArgumentsSource, and choose the one that keeps test data readable.

Fix Mockito errors about final classes, records and static methods by using the inline mock maker, and decide when a final class should be an interface.

Generate tests at runtime in JUnit with TestFactory, DynamicTest and DynamicContainer, and learn how they differ from parameterized tests.

Use JUnit Nested classes to group tests by state or behaviour, share setup with scoped BeforeEach, and keep large test classes readable.