Skip to content

unruly/junit-rules

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

junit-rules

Build Status

A collection of useful JUnit rules from Unruly's codebases

Install from Maven Central

<dependency>
    <groupId>co.unruly</groupId>
    <artifactId>junit-rules</artifactId>
    <version>1.1</version>
</dependency>

Ignore tests until a certain date or datetime.

This allows you to write an acceptance/integration test before implementing a feature, and integrate it into your codebase before the implementation is complete.

@IgnoreUntil must be present on the test method you wish to ignore.

The date/datetime value of the class level annotation can be shared across methods in the class or overridden by the method annotation.

@IgnoreUntil("2099-01-01")
public class MyIgnorableTest {

    @Rule public IgnoreUntilRule rule = new IgnoreUntilRule();

    @IgnoreUntil
    @Test
    public void ignoredUntil20990101() {
    }

    @IgnoreUntil
    @Test
    public void alsoIgnoredUntil20990101() {
    }

    @IgnoreUntil("2014-10-30")
    @Test
    public void ignoredUntil20141030() {
    }

    @IgnoreUntil("2014-10-30T17:30:00")
    @Test
    public void ignoredUntil20141030T173000() {
    }

    @Test
    public void notIgnored() {
    }
}

The class annotation is optional, you can just annotate the method.

public class MyIgnorableTest {

    @Rule public IgnoreUntilRule rule = new IgnoreUntilRule();

    @IgnoreUntil("2014-10-30T17:30:00")
    @Test
    public void ignoredUntil20141030T173000() {
    }

    @Test
    public void notIgnored() {
    }
}

Quarantine non-deterministic tests

@Rule QuarantineRule rule = new QuarantineRule();

@NonDeterministic(retries=3)
public void some_sporadically_failing_test() {

}

QuarantineRule supports a functional interface called QuarantineRuleLogger as a constructor argument for additional logging capabilities. For example, we email ourselves failures so it's harder to ignore.

@Rule QuarantineRule rule = new QuarantineRule(msg -> System.err.println(msg));

Make sure tests pass reliably

We use this to diagnose tests as being non-deterministic. To run each test 10 times:

@Rule ReliabilityRule rule = new ReliabilityRule(10);