0

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?

1
  • We will need to see some code to answer #2. Commented Nov 5, 2015 at 13:36

3 Answers 3

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, 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.

Sign up to request clarification or add additional context in comments.

4 Comments

If multiple test functions call persistFiles(), are they calling this function synchronously ?
Potentially (however Junit does not by default). However (assuming that MyFileManager is not a singleton) they would each have a different instance of the MyFileManager. You should also use something like TemporaryFolder so each test gets its own space to play.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@DJClayworth I disagree, this answers part one of the question. It does not however answer part two. I've asked for clarification on part two as a comment.
0
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?

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.