From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

TempDir

TempDir

- [Instructor] If you're working with files or directories in your tests, the TempDir annotation can prove to be a very useful tool. TempDir allows you to create and clean up temporary directories for your test methods. With TempDir, each test gets its own directory and these directories are automatically deleted after the test. This ensures that the file system remains clean and prevents interference between tests. Let's take a look at an example. Here, we have a FileProcessor class that copies the contents of the input file to create a new file with the contents uppercased. We test this with a FileProcessorTest class. Inside of here, there's a test case for a file containing content, as well as a test case for an empty file. In this example, we manually handle the file creation and cleanup, but we can actually have JUnit do this for us if we use TempDir. Let's modify this example to use the TempDir annotation. First, we'll get rid of the before and AfterEach methods. These are no…

Contents