Setting Fixed Joda Time In Unit Tests

in #programming8 years ago

Joda time is a very powerful time/date framework for Java. A commonly needed functionality is to obtain current time in your code. Current time can be obtained by creating a new instance of a corresponding class with a parameterless constructor. For example:

DateTime currentDateTime = new DateTime();
LocalDateTime currentLocalDateTime = new LocalDateTime();

Or with the static method now():

DateTime currentDateTime = DateTime.now();
LocalDateTime currentLocalDateTime = LocalDateTime.now();

Writing unit tests for code which is using this functionality would normally be very hard (if not impossible). Luckily for us though, the authors of Joda time have thought of this and introduced a handy method DateTimeUtils.setCurrentMillisFixed(...). Using this method we are able to set a fixed number of milliseconds since the “epoch time” (1970-01-01 00:00:00.000) to Joda. When this fixed number is set, every method for obtaining the current time will return an instance representing the fixed time.

Although this is great, setting the fixed time in milliseconds isn’t very convenient, but we can easily create methods that convert any Joda class instance into milliseconds and pass them to DateTimeUtils. If we go even further, we can create a nifty class, which can be used in all of our tests:

package com.example;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.LocalDateTime;

public class JodaTimeUtils {

    public static void setFixedCurrentTime(LocalDateTime fixedTime) {
        DateTimeUtils.setCurrentMillisFixed(fixedTime.toDateTime().getMillis());
    }

    public static void setFixedCurrentTime(DateTime fixedTime) {
        DateTimeUtils.setCurrentMillisFixed(fixedTime.getMillis());
    }

    // more methods can be created for other Joda classes

}

The usage is pretty straight-forward:

@Test
public void testSomeTimeDependentFunctionality() {

    DateTime fixedTime = new DateTime(2016, 3, 20, 14, 12);
    JodaTimeUtils.setFixedCurrentTime(fixedTime);

    ...
}
Sort:  

Congratulations @jardo! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published 4 posts in one day

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @jardo! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard!


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @jardo! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.033
BTC 116555.50
ETH 4489.25
SBD 0.86