title: How to mock Calendar.getInstance method using JMockit?
body: <p>I am trying to mock <code>Calendar.getInstance()</code> method using JMockit. This is the first time I am mocking anything.</p>
  
  <p>Below is my method which gives me the date of either recent <code>Monday</code> or recent <code>Thursday</code> in the format of <code>YYYYMMDD</code>.</p>
  
  <p>Today is Sunday, so it should return date for Thursday in the format of <code>YYYYMMDD</code> so it will be - <code>2014027</code></p>
  
  <pre><code>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());
  }
  </code></pre>
  
  <p>So my question is how do I mock the <code>Calendar.getInstance</code> method using JMokcit so that I can easily junit <code>getCorrectDate</code> method? </p>
  
  <p>I have never mocked it before so I am facing some problem?</p>
  
  <p>Can anyone provide a simple example if possible on my example?</p>
  
  <p>I am trying to do it like this but it is complaining me on <code>MockClass</code> as it is not able to resolve.. I have added the maven dependency of JMockit as well</p>
  
  <pre><code>@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;
   }
  }
  </code></pre>