Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Joda-Time library provides us a Virtual Clock utility named "DateTimeUtils". This is a demonstration for mocking start-end times.
import org.joda.time.DateTime;
public class StartEndTimeWithJodaTime {
private DateTime startTime;
private DateTime endTime;
public void process() {
startTime =;
// some heavy processing
endTime =;
public DateTime getStartTime() {
return startTime;
public DateTime getEndTime() {
return endTime;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.After;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class StartEndTimeWithJodaTimeTest {
public void saveStartEndTimeOfProcessing() {
final DateTime now = new DateTime(1989, 7, 4, 1, 15);
final DateTime fiveMinutesLater = now.plusMinutes(5);
// We need mock millis provider, because we need to return different times for each calls
DateTimeUtils.MillisProvider mockMillisProvider = mock(DateTimeUtils.MillisProvider.class);
StartEndTimeWithJodaTime processor = new StartEndTimeWithJodaTime();
assertThat(processor.getStartTime(), equalTo(now));
assertThat(processor.getEndTime(), equalTo(fiveMinutesLater));
public void after() {
// Use this to reset mock millis provider to system millis provider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.