0

I've got a unit test class where it needs to load in a file from resources. So I've got something like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class MyClassTest {

    private File resourceFile;

    @Before
    public void setup() {

        // The first time this is called, resourceFile is set up correctly
        // However, the second time, the call to getResource() returns a null and thus
        // an exception is thrown.
        if (resourceFile == null) {
            resourceFile = new File(getClass().getResource("/file.wav").getPath());
        }
    }

    @Test
    public void firstTest() {
        // resourceFile is available here
    }

    @Test
    public void secondTest() {
        // resourceFile is null here
    }
}

The problem is that the file from resources can be found the first time setup() is called, but oddly, when the second call to setup() happens, resourceFile is null again (That's another mystery to me; in my mind, I would think that should already be set up) so it has to be set up again, but then call to getResource() returns null, and thus an exception is thrown. It's almost like the whole MyTestClass is reset in between @Test calls. Even initializing resourceFile outside of the @Before method doesn't work either.

I'm somewhat new to unit testing, so if anybody can shed light on this issue, that would be great.

BrianP
  • 1,857
  • 6
  • 20
  • 27
  • 2
    Hi, junit creates a new instance of the test class for every test. Take a look to [this question](http://stackoverflow.com/questions/19381352/does-junit-reinitialize-the-class-with-each-test-method-invocation). For your test case, i recommend you use '@BeforeClass' (static) instead '@Before'. Concerning to getResource() nullPointer, I've been unable to reproduce it (I've run exactly the same test and for me resourceFile is available in both tests) – troig Sep 26 '14 at 07:31
  • 2
    Do any of your tests delete the resource file? – Duncan Jones Sep 26 '14 at 11:00
  • Is ii really related to powermock, did you try with a junit only test ? – gontard Sep 26 '14 at 12:55
  • Ugh! I just got to wondering about if the file was deleted, and @Duncan pointing that out as well got me to check up on that, and sure enough, deep in the class under test, there it was, the file being deleted. What I was doing was sending a loaded test resource file off to the class under test, but there's a delete in there as part of cleanup. Also, it's good to know that the test class is reinitialized for every Test. Thanks for pointing that out! – BrianP Sep 26 '14 at 13:14

1 Answers1

1

Since the tests delete your source file, copy the resource to a temporary location before running the test. This is good practice anyway, to avoid corrupting test resources.

Take a look at using JUnit's temporary file support: Working with temporary files in JUnit 4.7. You don't have to do it that way, but it's a neat solution.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254