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.