Hi Johan,
Thanks for the reply let me try to set an example:
Versions:
mockito-all-1.8.5
powermock-mockito-1.4-full
Suppose you have the following classes:
public class SampleWithStaticMethods {
private static int number=2;
public static int getTwo(){
return number;
}
public static int getFour(){
return 2 * number;
}
}
public class FirstTierSample {
public int getFour (){
return 2 * SampleWithStaticMethods.getTwo();
}
}
public class SecondTierSample {
public int getEight (){
FirstTierSample ftSample = new FirstTierSample();
return 2 * ftSample.getFour();
}
}
And you have the following test cases:
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("sample.SampleWithStaticMethods")
@PrepareForTest(SampleWithStaticMethods.class)
public class FirstTierSampleTest {
@Test
public void testGetFourMockedToGetSix() {
PowerMockito.mockStatic(SampleWithStaticMethods.class);
Mockito.when(SampleWithStaticMethods.getTwo()).thenReturn(3);
FirstTierSample testObject = new FirstTierSample();
assertEquals(6, testObject.getFour());
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(SampleWithStaticMethods.class)
public class ZecondTierSampleTest {
@Test
public void testGetEight() {
SecondTierSample testObject = new SecondTierSample();
assertEquals(8, testObject.getEight());
}
@Test
public void testGetEightMockedToGetTwelve() {
PowerMockito.mockStatic(SampleWithStaticMethods.class);
Mockito.when(SampleWithStaticMethods.getTwo()).thenReturn(3);
SecondTierSample testObject = new SecondTierSample();
assertEquals(12, testObject.getEight());
}
@Test
public void testGetFour(){
assertEquals(4, SampleWithStaticMethods.getFour());
}
}
Using eclipse if I run each test class individually they both run
perfectly if I run the package sample I fail two test cases form the
second test class, now if I run this from within ant (as a junit
batchtest task) all test cases will run correctly.
It looks like on ant the @SuppressStaticInitializationFor was being
reverted every time it runs a test class but not on eclipse.
I hope this clears out the questions.
Regards,
--Aaron