Note that there are some explanatory texts on larger screens.

plurals
  1. POMockito: How to mock an interface of JodaTime
    text
    copied!<p>I use <code>JodaTime#DateTime</code>, and I need to mock its behavior. Since it is not possible to directly mock <code>JodaTime#DateTime</code>, I create an interface of it</p> <p>Clock.java</p> <pre><code>public interface Clock { DateTime getCurrentDateTimeEST(); DateTime getFourPM_EST(); DateTime getSevenPM_EST(); } </code></pre> <p>JodaTime.java</p> <pre><code>public class JodaTime implements Clock { @Override public DateTime getCurrentDateTimeEST() { return new DateTime(DateTimeZone.forID("EST")); } @Override public DateTime getFourPM_EST() { DateTime current = getCurrentDateTimeEST(); return new DateTime(current.getYear(), current.getMonthOfYear(), current.getDayOfMonth(), 16, 0, 0, 0, DateTimeZone.forID("EST")); } @Override public DateTime getSevenPM_EST() { DateTime current = getCurrentDateTimeEST(); return new DateTime(current.getYear(), current.getMonthOfYear(), current.getDayOfMonth(), 19, 0, 0, 0, DateTimeZone.forID("EST")); } } </code></pre> <p>Here is the method that I want to test</p> <pre><code>public class PrintProcessor{ Clock jodaTime; public PrintProcessor(){ jodaTime = new JodaTime(); } ... public String getPrintJobName(Shipper shipper){ String printJobName = null; //Get current EST time if(jodaTime.getCurrentDateTimeEST().isBefore(jodaTime.getFourPM_EST()) || jodaTime.getCurrentDateTimeEST().isAfter(jodaTime.getSevenPM_EST())){ //Before 4PM EST and after 7PM EST switch(shipper){ case X: ... }else if(jodaTime.getCurrentDateTimeEST().isBefore(jodaTime.getSevenPM_EST())){ //Between 4PM-7PM EST switch(shipper){ case X: ... } return printJobName; } } </code></pre> <p>As you can see the <code>printJobName</code> depend on the current time of the day relative to the time interval [4PM-7PM] EST and the Shipper name. Since Shipper will be pass via parameter, we can unit test it no problem. But I need to mock the time. So here is what I try</p> <pre><code>@Test public void testGetPrintJobNameBeforeFourPM(){ DateTime current = new DateTime(DateTimeZone.forID("EST")); Clock clock = mock(Clock.class); //Always return 6pm when I try to ask for the current time when(clock.getCurrentDateTimeEST()).thenReturn(new DateTime(current.getYear(), current.getMonthOfYear(), current.getDayOfMonth(), 18, 0, 0, 0, DateTimeZone.forID("EST"))); //Test for Fedex String printJobName = printProcessor.getPrintJobName(Shipper.X); assertEquals("XNCRMNCF", printJobName); } </code></pre> <p>The test should fail since I pass in 6PM, but <code>XNCRMNCF</code> is the name for before 4PM. Do I need to mock <code>printProcessor</code> as well. If what I have is wrong. How should I fix it? <strong>I am trying to learn writing high level java code, please be very criticized about my code. I really want to learn</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload