Jodatime

Installation

If you are using maven, you can add following dependency to your project.

01
02
03
04
05
<dependency>
    <groupId>org.unitils.jodatime</groupId>
    <artifactId>unitils-jodatime</artifactId>
    <version>1.0.3</version>
</dependency>

Config

Please create unitils-local.properties, and add mail to unitils.modules. Code as following:

01
02
03
04
05
unitils.modules= database, easymock, datetime
 
unitils.module.datetime.className = org.unitils.jodatime.JodaTimeModule
unitils.module.datetime.runAfter =
unitils.module.datetime.enabled = true

Example

How does it work?
There are 2 annotations:

  • FixedDateTime: It freezes the date from a specific date.
  • OffsetDateTime: It freezes the date from a specific amount of days/months/years from now (positive = future, negative = past).
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    @RunWith(UnitilsJUnit4TestClassRunner.class)
    @FixedDateTime(datetime = "30/01/2008")
    public class DateTimeExample1Test {
     
     
        @Test
        public void testTestClassDateTime() {
            // the test class defines the fixed datetime as 30/01/2008, so the current datetime should be equal to that.
            DateTime expected = new DateTime(2008, 1, 30, 0, 0, 0, 0);
            Assert.assertEquals(expected, new DateTime());
        }
     
        @Test
        @FixedDateTime
        public void testFixedDateTimeDefault() throws InterruptedException {
            DateTime expected = new DateTime(System.currentTimeMillis());
            DateTime actual = new DateTime();
     
            Assert.assertEquals(expected.getYear(), actual.getYear());
            Assert.assertEquals(expected.getMonthOfYear(), actual.getMonthOfYear());
            Assert.assertEquals(expected.getDayOfMonth(), actual.getDayOfMonth());
            Assert.assertEquals(expected.getHourOfDay(), actual.getHourOfDay());
            Assert.assertEquals(expected.getMinuteOfDay(), actual.getMinuteOfDay());
            Assert.assertEquals(expected.getSecondOfMinute(), actual.getSecondOfMinute());
     
        }
     
        @Test
        @FixedDateTime(datetime = "29/02/2020")
        public void testTestMethodDateTime() {
            // the test method defines the fixed datetime as 29/02/2020,
            //so the current datetime should be equal to that.
        }
     
        @Test
        @OffsetDateTime
        public void testCurrentDatetime() throws InterruptedException {
            //OffsetDateTime without value is the current datetime.
        }
     
        @Test
        @OffsetDateTime(days = 1)
        public void testOffsetDatetime() {
            //the current date should be date from tomorrow.
     
        }
    }