32

In my code I have something like this:

private void doSomething() {
   Calendar today = Calendar.getInstance();
   ....
}

How can I "mock" it in my junit test to return a specific date?

Randomize
  • 7,970
  • 18
  • 69
  • 122

6 Answers6

27

You can mock it using PowerMock in combination with Mockito:

On top of your class:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassThatCallsTheCalendar.class})

The key to success is that you have to put the class where you use Calendar in PrepareForTest instead of Calendar itself because it is a system class. (I personally had to search a lot before I found this)

Then the mocking itself:

mockStatic(Calendar.class);
when(Calendar.getInstance()).thenReturn(calendar);
GoGoris
  • 771
  • 7
  • 20
  • 1
    which dependencies do you include to get mockStatic() method work? – Leonid Ustenko Mar 13 '17 at 15:18
  • Sorry I should have said that. I used import static to import the mockStatic method of PowerMockito. See this dependency for powermock mockito: http://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2/1.6.6 – GoGoris Mar 14 '17 at 19:01
  • You made my day! – Anand Vaidya Sep 09 '18 at 14:55
  • +1 , but for the second part, both lines could be simplified to: `PowerMockito.whenNew(Calendar.getInstance()).withAnyArguments().thenReturn(MY_INSTANCE_OBJECT_TO_FEED);` – Payam Jun 25 '21 at 00:48
17

As far as I see it you have three sensible options:

  1. Inject the Calendar instance in whatever method/class you set that day in.

    private void method(final Calendar cal) { Date today = cal.getTime(); }

  2. Use JodaTime instead of Calendar. This is less an option and more a case of a suggestion as JodaTime will make your life a lot easier. You will still need to inject this time in to the method.

    DateTime dt = new DateTime();

    Date jdkDate = dt.toDate();

  3. Wrap Calendar inside some interface that allows you to fetch the time. You then just mock that interface and get it to return a constant Date.

    Date today = calendarInterfaceInstance.getCurrentDate()

BeRecursive
  • 6,146
  • 1
  • 22
  • 40
  • 4
    Joda Time's `DateTimeUtils` class has static methods that set the current time for all other Joda Time objects. This is very useful to set the time to a certain moment, for example for testing. – Jesper Feb 14 '12 at 12:16
  • @Jesper - Yes that is true and a good point I neglected to mention – BeRecursive Feb 14 '12 at 12:26
  • 1
    Thank you guys. I followed ur suggestions and I moved to JodaTime. BTW it fixes easily the problem with something like: DateTimeUtils.setCurrentMillisFixed(new DateTime(2012, 2, 14, 13, 43, 21).getMillis()); – Randomize Feb 14 '12 at 14:48
12

Don't mock it - instead introduce a method you can mock that gets dates. Something like this:

interface Utility {

    Date getDate();
}

Utilities implements Utility {


    public Date getDate() {

        return Calendar.getInstance().getTime();
    }

}

Then you can inject this into your class or just use a helper class with a bunch of static methods with a load method for the interface:

public class AppUtil {

    private static Utility util = new Utilities();

    public static void load(Utility newUtil) {

         this.util = newUtil;
    }

    public static Date getDate() {

        return util.getDate();
    }

}

Then in your application code:

private void doSomething() {
   Date today = AppUtil.getDate();
   ....
}

You can then just load a mock interface in your test methods.

@Test
public void shouldDoSomethingUseful() {
     Utility mockUtility = // .. create mock here
     AppUtil.load(mockUtility);

     // .. set up your expectations

     // exercise the functionality
     classUnderTest.doSomethingViaAPI();

     // ... maybe assert something 

}

See also Should you only mock types you own? and Test smell - everything is mocked

Community
  • 1
  • 1
blank
  • 17,484
  • 19
  • 98
  • 155
6

Using Mockito and PowerMockito:

Calendar endOfMarch = Calendar.getInstance();
endOfMarch.set(2011, Calendar.MARCH, 27);
PowerMockito.mockStatic(Calendar.class);
Mockito.when(Calendar.getInstance()).thenReturn(endOfMarch);

Refer to the link for the complete code.

Surekha
  • 522
  • 1
  • 7
  • 12
2

Write a class called DateHelper with a method getCalendar that returns Calendar.getInstance(). Refactor the class that you're testing so that it has a member variable of type DateHelper, and a constructor that injects that member variable. Use that constructor in your test, to inject a mock of DateHelper, in which getCalendar has been stubbed to return some known date.

Dawood ibn Kareem
  • 73,541
  • 13
  • 95
  • 104
1

You can mockit using JMockit. Here you can see how you can do it: Mock Java Calendar - JMockit vs Mockito.

Behrang
  • 44,452
  • 23
  • 114
  • 153