14

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?

user2586917
  • 762
  • 1
  • 9
  • 26

3 Answers3

13

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

Deencapsulation.setField(ClassA, "File", your_desired_value);
Martin Lane
  • 131
  • 1
  • 2
11

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.

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • Excellent, thanks a million for your help Rogerio, this worked with one small change to your code - @Mocked File myDir had to be changed to @Mocked(methods={"list"}) File myDir to get it to work. I think this was maybe because the Deencapsualtion.invoke call maybe needs a real File object at some point, and mocking out all the methods seemed to be interfering with something. I'm a new user so can't vote you up - otherwise I would! – user2586917 Jul 16 '13 at 13:27
  • 1
    Great! Mocking `File` can indeed cause unexpected failures, at least in older versions of JMockit. I edited the answer with your changes. – Rogério Jul 16 '13 at 15:07
  • @Rogério, the above way of creating a mock object (`myDir`) results in the reference being `null`, causing `myDir.list()` to fail with an `NPE`. Any idea why? – mystarrocks Sep 09 '14 at 15:44
  • @mystarrocks Local mock fields are not supported anymore. The test was updated to use a mock parameter instead. – Rogério Sep 09 '14 at 17:07
  • Ah I see. For some reason, I hate listing a bunch of mocks other than the maybe 3 at most, through `@Mocked` in the test method signature. I'd rather have preferred this way of using mocks. – mystarrocks Sep 09 '14 at 17:46
  • 1
    Don't use actual files. Doing so engages a file *system* which adds race conditions and makes normally fast executing unit tests that execute in .01s into slow tests that execute into .5s. (Now imagine having a thousand unit tests and you'll see that you'll have a slow feedback loop which will ruin the point of having fast executing unit tests to start with.) – Lance Kind Feb 23 '16 at 04:31
-1

try out this:

new Expectations {{
  invoke(File.class, "list", null, null);
  returns(files);
}}
jdevelop
  • 12,176
  • 10
  • 56
  • 112
  • 1
    Thanks, but the docs say invoke is for invoking a static method - list() on File is not static - doesn't seem to work. – user2586917 Jul 16 '13 at 13:01