I have a mocking problem Calendar.getInstance() . Since now this method returns Calendar - an object that I am mocking.
Now my code is as follows:
@RunWith(PowerMockRunner.class) @PrepareForTest(Calendar.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(); } }
Calendar.getInstance() is called in different ways in surveillance.checkDatabase() and each time it is a new object, rather than the expected Calendar layout.
Can anyone see what I'm doing wrong?
source share