Joann - Joda Annotation

Allows for trivial setting of Joda time in unit tests

License

License

GroupId

GroupId

com.elevenware
ArtifactId

ArtifactId

joann
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Joann - Joda Annotation
Allows for trivial setting of Joda time in unit tests
Project URL

Project URL

https://github.com/georgecodes/joann
Source Code Management

Source Code Management

http://github.com/georgecodes/joann

Download joann

How to add to project

<!-- https://jarcasting.com/artifacts/com.elevenware/joann/ -->
<dependency>
    <groupId>com.elevenware</groupId>
    <artifactId>joann</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.elevenware/joann/
implementation 'com.elevenware:joann:1.0'
// https://jarcasting.com/artifacts/com.elevenware/joann/
implementation ("com.elevenware:joann:1.0")
'com.elevenware:joann:jar:1.0'
<dependency org="com.elevenware" name="joann" rev="1.0">
  <artifact name="joann" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.elevenware', module='joann', version='1.0')
)
libraryDependencies += "com.elevenware" % "joann" % "1.0"
[com.elevenware/joann "1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
junit : junit jar 4.10
xmlunit : xmlunit jar 1.2
joda-time : joda-time jar 1.6.2

runtime (1)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 1.8.1

test (1)

Group / Artifact Type Version
org.spockframework : spock-core jar 0.6-groovy-1.7

Project Modules

There are no modules declared in this project.

#JoAnn

Build Status

JoAnn - Joda Annotation - makes the use of Joda time in your tests easier, and gets boilerplate out of the way.

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? It's now in Maven central.

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

Enjoy.

Versions

Version
1.0