Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock Calendar.getInstance method using JMockit?

I am trying to mock Calendar.getInstance() method using JMockit. This is the first time I am mocking anything.

Below is my method which gives me the date of either recent Monday or recent Thursday in the format of YYYYMMDD.

Today is Sunday, so it should return date for Thursday in the format of YYYYMMDD so it will be - 2014027

public static String getCorrectDate() {

    Calendar cal = Calendar.getInstance();

    try {
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        switch (dow) {
        case Calendar.THURSDAY:
        case Calendar.FRIDAY:
        case Calendar.SATURDAY:
        case Calendar.SUNDAY:
        while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY) {
            cal.add(Calendar.DATE, -1);
        }
        break;
        case Calendar.MONDAY:
        case Calendar.TUESDAY:
        case Calendar.WEDNESDAY:
        while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            cal.add(Calendar.DATE, -1);
        }
        break;
        }
    } catch (Exception ex) {
        // log error
    }

    return toDateFormat.format(cal.getTime());
}

So my question is how do I mock the Calendar.getInstance method using JMokcit so that I can easily junit getCorrectDate method?

I have never mocked it before so I am facing some problem?

Can anyone provide a simple example if possible on my example?

I am trying to do it like this but it is complaining me on MockClass as it is not able to resolve.. I have added the maven dependency of JMockit as well

@MockClass(realClass = Calendar.class) 
class CalendarMock {
 private int hour;
 private int day;
 private int minute;

 public CalendarMock(int day, int hour, int minute) {
  this.hour = hour;
  this.day = day;
  this.minute = minute;
 }

 @Mock
 public int get(int id) {
  if (id == Calendar.HOUR_OF_DAY) {
   return hour;
  }
  if (id == Calendar.DAY_OF_WEEK) {
   return day;
  }

  if (id == Calendar.MINUTE) {
   return minute;
  }
  return -1;
 }
}

1 Answers

public class CalendarTest {

    @Test
    public void testCalender() {

        new MockCalendar(1,2,3);

        Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.HOUR_OF_DAY);
        Assert.assertEquals(day, 2);
    }

  public static class MockCalendar extends MockUp<Calendar> {
        private int hour;
        private int minute;
        private int day;

        public MockCalendar(int day, int hour, int minute) {
            this.hour = hour;
            this.minute = minute;
            this.day = day;
        }
        @Mock
        protected void $init() {}

        @Mock
        public static Calendar getInstance() {
            return new GregorianCalendar(1970,1,1);
        }

        @Mock
        public int get(int id) {
            if (id == Calendar.HOUR_OF_DAY) {
                return hour;
            }
            if (id == Calendar.DAY_OF_WEEK) {
                return day;
            }

            if (id == Calendar.MINUTE) {
                return minute;
            }
            return -1;
        }
    }
 }

}

you can enhance the get logic to do stuff.

like image 187
Russel Yang Avatar answered Sep 16 '26 19:09

Russel Yang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!