Issue
I am fairly new with Junit and I need to write JUnit 5 test case for a method which has code statement:
public void method() {
// some code
Files.write(filepath)
// some more code
}
- Will this code create new files every time when unit test is run ?
- Is there a way to write a unit test for this scenario where files are not being created, or get deleted automatically once the test method execution is complete ?
Solution needs to be JUnit 5 compatible.
Solution
I have used JUnit 5's Temporary Directory(@TempDir
) functionality to create a temporary directory and then pass the directory path to Files.write()
method (along with the file name of course). The @TempDir
will delete the files and temporary folder, it created, once the test execution is done. This way, I was able to execute the test case.
Answered By - Navpreet Singh