Question

I have a class like the following;

class ClassA {
    private static File myDir;

    // myDir is created at some stage

    private static String findFile(final String fileName) {
       for (final String actualBackupFileName : myDir.list()) {
           if (actualBackupFileName.startsWith(removeExtensionFrom(backupFile))) {
               return actualBackupFileName;
            }
       }
    }
}

So, basically, I want to test this class by mocking out the File class so that when list() is called on it it returns a list of strings I define in my test class.

I've got the following but its not working at the minute, there's probably something obvious I'm doing wrong - I'm new to JMockit - any help is much appreciated!

@Mocked("list") File myDir;

@Test
  public void testClassA() {
    final String[] files = {"file1-bla.txt"};

    new NonStrictExpectations() {{
      new File(anyString).list(); 
      returns(files);
   }};

   String returnedFileName = Deencapsulation.invoke(ClassA.class, "findFile","file1.txt");

   // assert returnedFileName is equal to "file1-bla.txt"
  }

When running the above test I get a NullPointerException for the myDir field in ClassA - so it looks like its not getting mocked properly?

Was it helpful?

Solution 2

JMockit (or any other mocking tool) does not mock fields or variables, it mocks types (classes, interfaces, etc.) Where the instances of those types get stored inside the code under test is not relevant.

Example test for ClassA:

@Test
public void testClassA(@Mocked File myDir)
{
    new Expectations() {{ myDir.list(); result = "file1-bla.txt"; }};

    String returnedFileName = new ClassA().publicMethodThatCallsFindFile("file1.txt");

    assertEquals("file1-bla.txt", returnedFileName);
}

The above should work. Note that testing private methods directly (or accessing private fields) is considered bad practice, so I avoided it here. Also, it's best to avoid mocking the File class. Instead, test only your public methods, and use actual files instead of mocking the filesystem.

OTHER TIPS

You can use the setField method from the Deencapsulation class. Note example below:

Deencapsulation.setField(ClassA, "File", your_desired_value);

try out this:

new Expectations {{
  invoke(File.class, "list", null, null);
  returns(files);
}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top