Details

Recommendation type: Files created

Configurable in Preferences: Yes

Filter category: Unstable test environment

Description

UTA displays this recommendation when a newly created file fails to be deleted by the time the test has completed. Failing to delete files created on the file system may cause subsequent test runs or other tests to fail.

(info) The recommendation is not displayed when the file to be created already exists when the test is run.

Example

In the following example, a new file is created, but not removed.

public class MyTest {
    private File _testFile;
    private static final String PATH = "newFile";
   
    public void setUp() throws IOException {
        // Prepare test environment
        _testFile = new File(PATH);
        _testFile.createNewFile();
    }
} 

Repair

The created file is removed after the test is executed.

public class MyTest {
    private File _testFile;
    private static final String PATH = "newFile";
     
    @Before
    public void setUp() throws IOException {
        // Prepare test environment
        _testFile = new File(PATH);
        _testFile.createNewFile();
    }
 
    @After
    public void tearDown() {
        // Remove test related files
        _testFile.delete();
    }
}
  • No labels