I have a problem mocking Calendar.getInstance()
. As you now this method returns a Calendar - the object I am mocking.
Right now my code looks like this:
It seems that you need to add the target test class in the PrepareForTest tag:
@PrepareForTest({ Calendar.class, Surveillance.class })
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Calendar.class, Surveillance.class })
public class SurveillanceDatabaseTest {
@Test
public void testFailingDatabase() throws Exception {
mockStatic(Calendar.class);
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.HOUR, 1);
when(Calendar.getInstance()).thenReturn(calendar);
final Surveillance surveillance = new Surveillance();
surveillance.checkDatabase();
}
}
Even Tom Tresansky's example above will need it if we move the Surveillance class to somewhere outside MockCalendarTest class.
It seems like you're doing everything right. For instance, this test below passes, proving that the Calendar
returned by Calendar#getInstance()
is in fact the one you set up with the static mocking.
import static org.junit.Assert.*;
import static org.powermock.api.mockito.PowerMockito.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Calendar.class)
public class MockCalendarTest {
@Test
public void testFailingDatabase() {
mockStatic(Calendar.class);
final Calendar testCalendar = new GregorianCalendar();
testCalendar.add(Calendar.HOUR, 1);
when(Calendar.getInstance()).thenReturn(testCalendar);
final Surveillance surveillance = new Surveillance();
final Calendar resultCalendar = surveillance.checkDatabase();
assertTrue(testCalendar == resultCalendar);
}
public static class Surveillance {
public Calendar checkDatabase() {
return Calendar.getInstance();
}
}
}
Perhaps post the relevant parts of the Surveillance
class so we can see how it's trying to get a new Calendar
and assess why it's failing.
I'm not as familiar with the when(object.call()).andReturn(response);
but I'm assuming it works the same way as expect.(object.call()).andReturn(response);
. If that is the case, then it looks like all you are missing a replay of the class PowerMock.replay(Calendar.class)
and you are trying to do a full static mock instead of a partial static mock. This should resolve your issue.
@RunWith(PowerMockRunner.class)
@PrepareForTest(Calendar.class)
public class SurveillanceDatabaseTest {
@Test
public void testFailingDatabase() throws Exception {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.HOUR, 1);
PowerMock.mockStaticPartial(Calendar.class, "getInstance"); //Mock Static Partial
expect(Calendar.getInstance()).andReturn(calendar);
PowerMock.replay(Calendar.class); // note the replay of the Class!
final Surveillance surveillance = new Surveillance();
surveillance.checkDatabase();
//Whatever tests you need to do here
}
}