PowerMocking static does not return the expected object

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?

+4
source share
3 answers

It seems you need to add the target test class to 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(); } } 

The above example, Tom Trezansky, is needed if we move the Surveillance class somewhere outside of the MockCalendarTest class.

+1
source

It seems you are doing everything right. For example, this test passes below, proving that the Calendar returned by Calendar#getInstance() is actually the one you installed with static bullying.

 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 that we can see how it tries to get the new Calendar and evaluate why it does not work.

0
source

I am not familiar with when(object.call()).andReturn(response); but I assume that it works the same as expect.(object.call()).andReturn(response); . If so, then it looks like everything that you are missing in playing the PowerMock.replay(Calendar.class) class PowerMock.replay(Calendar.class) , and you're trying to make a full static layout instead of a partial static layout. This should solve your problem.

 @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 } } 
0
source

All Articles