user842225
user842225

Reputation: 5979

JUnit test case failed

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:

  1. I don’t understand, it is failed at testGetFiles() why I can still see the print testPersistFiles() which sounds like even it is failed, it doesn't stop running, but continues to run the next test testPersistFiles()? What is happening behind the scene in JUnit test case??

  2. Another thing I don’t understand is why testGetFiles() is failed? I can see log that the persistFiles() has persisted files. Why it got null after that?

Upvotes: 0

Views: 94

Answers (3)

Michael Lloyd Lee mlk
Michael Lloyd Lee mlk

Reputation: 14661

I don’t understand, it is failed at testGetFiles() why I can still see the print testPersistFiles() which sounds like even it is failed, i

That is how unit testing works. Each test should be isolated and working using only its set of data. Unit test frameworks run every test so you can see which parts of the system work and which do not, they do not stop on the first failure.

Upvotes: 1

shrini1000
shrini1000

Reputation: 7226

For each of your tests, JUnit creates a separate instance of that class and runs it. Since you seem to have 3 tests, JUnit will create 3 instances of your class, execute @Before on each of them to initialize state, and then run them. The order in which they are run is typically the order in which the tests are written but this is not guaranteed.

Now about the print statement - you see that it's the first statement in your test so it will be executed. Then mFileManager.persistFiles(); is executed. For some reason it returns a false and hence the test fails.

As to why it returns false, you can run a local debugger, put a break point at the beginning of that method, single-step and see.

Upvotes: 0

aurelius
aurelius

Reputation: 4076

mFileManager.getFilesAtPath(dirPath);

You are not searching the files in the right place

String dirPath = “/myDir/”

Are you sure that this path is ok? with a slash before the directory name?

Upvotes: 0

Related Questions