Reputation: 326
I have a legacy class which has a static void method which i need to test:
public class A {
public static void renameTo()
{
String ext = "." + this.fileName + ".backup";
for (File file : getCSVFile()) {
f.renameTo(new File(file.getAbsolutePath() + ext));
}
public static File[] getAllFiles()
{
//logic to read the CSV files from the class path
}
}
Now I have written a test case for it using PowerMockito which looks like this. Now the issue is, even though the renameTo()
is called only, if i call PowerMockito.verifyStatic( Mockito.times(10))
the test still passes
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
@PowerMockIgnore("javax.management.*")
public class ATest {
@Test
public void testRenameTo() throws Exception {
PowerMockito.mockStatic(A.class);
A.renameTo();
PowerMockito.verifyStatic( Mockito.times(1));
//PowerMockito.verifyStatic( Mockito.times(5));//Passes even though the mehod is called only once
//PowerMockito.verifyStatic( Mockito.times(10);//Passes even though the mehod is called only once
}
}
Could someone please shed some light into this issue? what I may be doing wrong?
Upvotes: 3
Views: 2820
Reputation: 1904
As per the documentation, after the test verifyStatic
needs to be called first, then call A.renameTo()
to tell it which static method to verify. Example:
// run test
A.renameTo();
// verify interaction
PowerMockito.verifyStatic(A.class, Mockito.times(1));
A.renameTo();
Upvotes: 3