user1883212
user1883212

Reputation: 7859

Unit test for getting current UTC date-time

I have a method like this:

public Date getCurrentUtcDateTime() {
        // Return UTC time
}

I want to create a unit test with JUnit for it.

assertEquals( ?????? 

What's the right approach?

  1. If I write unit test code to calculate the UTC current time, it's not good because I would essentially be rewriting the entire function. In addition to this the milliseconds of the two datetimes will not be the same.

  2. If I just check that the result is not null, it's not good - the result might still be wrong.

What else should I try? What's the right way to write a unit test for this function?

Upvotes: 0

Views: 2069

Answers (2)

John B
John B

Reputation: 32959

So I have come across this multiple times. And in a lot of cases getting the before and after time (as suggested) works well. However, I have had problems with this in cases where the time is converted to a String or some such where I need the exact time to be able to create a good test. For this reason I created a mechanism to control time (just not the space-time continuum).

I have a class called DateSupplier which has methods like getCurrentTime() and getCurrentDateTime. The default behavior returns new Date().

I then have a JUnit Rule DateController that can be used to control explicitly what DateSupplier returns. The DateController Rule is in a test library so should not be deployed with production code. After each test it sets the behavior of DateSupplier back to the default behavior of returning the correct current Date.

public Date getCurrentUtcDateTime() {
    // Return UTC time
     Date date = DateSupplier.getCurrentDate();
}

public class MyTest{

     @Rule
     public DateController dateController = new DateController();

     private Date currentTime;

     @Before
     public void setup(){
        dateController.setCurrentTimeToNow();
        currentTime = DateSupplier.getCurrentTime();
     }

     @Test
     public void myTest(){
        Date value = getCurrentUtcDateTime();

        // verify
        Date utcDate = // convert currentTime to UTC
        assertThat(value, equalTo(utcDate));
     }
}

Upvotes: 0

Duncan Jones
Duncan Jones

Reputation: 69349

If I write unit test code to calculate the UTC current time, it's not good because I would essentially be rewriting the entire function

Sometimes that's the only way you can test something. Perhaps there is another approach you can use to get the same time? I.e. use Joda-time not standard Java classes, or vice versa.

One approach for testing could be (psuedo-code):

startTime = getUTCTimeUsingAlternativeMethod();
resultTime = classUnderTest.getCurrentUtcDateTime();
endTime = getUTCTimeUsingAlternativeMethod();

validateThat(startTime <= resultTime);
validateThat(resultTime <= endTime);

But as Jon points out in the comments, a method this simple may not require testing. Your time may be better spent testing other methods in more detail.

Upvotes: 1

Related Questions