George McIntosh

I do something...with software...

JoAnn

Writing some tests recently, as is often the case, I found myself reaching for Joda Time. This library, as most Java devs know, is less painful to deal with than the date utilities that the JDK ships with. The other neat thing about it is, you can set the system time - well, Joda’s view of it - to whatever you want. Very handy for testing code that might at some point create dates.

Anyways, a typical way of using Joda time in a test looks something like this

class TestSomething {

    @Test
    void testthings() {

        DateTimeUtils.setCurrentMillisFixed(1385554748577L)

        // do some things

    }

    @After
    void reset() {

        DateTimeutils.setCurrentMillisSystem()

    }

}

A few things wrong here. First, it’s tedious having to work in milliseconds. Second it’s not especially legible, and thirdly you have to remember to reset the time to the real time in a teardown method in case your test fails.

So I came up with JoAnn, which makes things a bit simpler. JoAnn is so called because it’s a Joda Annotation. It looks a bit like this

@Test
@Joda(1385554748577L)
void testthings() {
    // do some things
}

That’s (almost) it. No time fiddling bleeding into your tests, no need to remember to tear it down. Just the annotation. Oh, and a way of getting it invoked. You can use either a JUnit runner, or a JUnit rule, it makes no difference. I’ve provided both

With a rule

class MyTests {

  @Rule public JodaRule rule = new JodaRule()

}

With a runner

@RunWith(JodaAwareJUnit4Runner)
class MyTests {

}

It gets even less tedious.

You don’t have to use milliseconds to set the time. Set a timestamp instead, and JoAnn will assume you meant an ISO8601 format

@Test
@Joda(timestamp = "2013-11-29T10:13:22.192Z")
void testTheThings() {}

Or there are a few more out of the box:

@Test
@Joda(timestamp = '2013-12-25', format = Format.YYYYMMDD)
void testMoreStuff() {}

@Test
@Joda(timestamp = '2012-11-19 13:03:22', format = Format.YYYYMMDD_HHMMSS)
void keepOnTesting() {}

Want to see the code? It’s on GitHub

Want to use it in a Maven project? I’m hosting it myself for now:

<dependencies>
  <dependency>
        <groupId>com.elevenware</groupId>
        <artifactId>joann</artifactId>
        <version>1.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>elevenware-releases</id>
        <url>http://maven.elevenware.com/repo/releases</url>
    </repository>
</repositories>

Enjoy.

Comments