I have a simple test case:
public class FileManagerTest { String dirPath = “/myDir/” @Before public void setUp() { mFileManager = MyFileManager.getInstance(); } @Test private void testPersistFiles() { System.out.println(“testPersistFiles()…”); //it deletes old files & persists new files to /myDir/ directory boolean successful =mFileManager.persistFiles(); Assert.assertTrue(successful); } @Test public void testGetFiles() { System.out.println(“testGetFiles()…”); mFileManager.persistFiles(); //I double checked, the persistFiles() works, the files are persisted. List<File> files = mFileManager.getFilesAtPath(dirPath); Assert.assertNotNull(files); //Failure here!!!! } @Test public void testGetFilesMap() { System.out.println(“testGetFilesMap()…”); mFileManager.persistFiles(); Map<String, File> filesMap = mFileManager.getFilesMapAtPath(dirPath); Assert.assertNotNull(files); } } The persistFiles() function in FileManager delete all files under /myDir/ then persist files again.
As you see above, I have a System.out.println(…) in each test function. When I run it , I can see all the prints in the following order:
testGetFilesMap()… testGetFiles()… testPersistFiles()… However, test is failed at testGetFiles(). Two things I don't understand:
I don’t understand, it is failed at
testGetFiles()why I can still see the printtestPersistFiles()which sounds like even it is failed, it doesn't stop running, but continues to run the next testtestPersistFiles()? What is happening behind the scene in JUnit test case??Another thing I don’t understand is why
testGetFiles()is failed? I can see log that thepersistFiles()has persisted files. Why it got null after that?