Useful JUnit Helper Method: ignoreUntil()

We all know the pattern: A test fails but you can’t fix it right away. What do you do? Let it stay red? @Ignore it?

All those approaches have drawbacks. Red tests make me nervous. But when I add an @Ignore, I sometimes forget to remove it in a timely fashion. Enter stage ignoreUntil():

/**
 * Ignore a test until a certain date.
 *
 * <p>This method will make the test silently fail
 * (as if it had been ignored) until a certain
 * date.
 *
 * <p>For documentation purposes, you can give a
 * message that explains why it's ignored.
 */
public static void ignoreUntil(
    String date_YYYY_MM_DD,
    String message
) {
    Date date;
    SimpleDateFormat format
        = new SimpleDateFormat( "yyyy-MM-dd" );
    try {
        date = format.parse( date_YYYY_MM_DD );
    } catch( ParseException e ) {
        throw new RuntimeException(
            "Can't parse date [" 
            + date_YYYY_MM_DD
            + "] using format "
            + format.toPattern()
        );
    }
    Assume.assumeTrue( new Date().after( date ) );
}

Usage:

@Test
public void someTest() throws Exception {
    ignoreUntil( "2011-09-15", "See issue #78" );
}

Happy testing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: