Test support classes and utilities for Java.
Java
Switch branches/tags
Nothing to show
Pull request Compare This branch is 16 commits behind danhaywood:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
testsupport
.gitignore
README.md

README.md

A collection of test support classes and utilities for Java.

ValueContractTestAbstract

Use to easily unit test value types, namely the equals() and hashCode() methods. All the usual rules are tested.

For example:

public class ValueTypeContractTestAbstract_BigIntegerTest
        extends ValueTypeContractTestAbstract<BigInteger> {
    @Override
    protected List<BigInteger> getObjectsWithSameValue() {
        return Arrays.asList(new BigInteger("1"), new BigInteger("1"));
    }
    @Override
    protected List<BigInteger> getObjectsWithDifferentValue() {
        return Arrays.asList(new BigInteger("2"));
    }
}

For further discussion, see this blog post

PojoTester

Use to exercise all the getters and setters of a pojo. All the main value types in the JDK are supported (int, String, java.util.Date etc), as well as enums; extensible to support any other value type.

For example:

new PojoTester().exercise(new Customer());

PrivateConstructorTester

Use to instantiate classes that have a hidden constructor, (to increase code coverage).

Typically this is used for classes holding a bunch of constants:

public final class Constants {
    private Constants() {}
    public final static FOO = 1;
    public final static BAR = 2;
}

For example:

new PrivateConstructorTester(Constants.class).exercise();

JUnitRuleMockeryTest

A combination of a JMock Mockery with a JUnit rule, providing support for Mockito-like @Mock annotation, along with optional auto-wiring support of the class under test (identified by a new @ClassUnderTest annotation). Additionally, expectations can be set on the mock using @Ignoring, @Allowing, @Never, @One or using the general-purpose @Checking(Expectations.class) annotation.

For example:

public class JUnitRuleMockery2Test_autoWiring {

    @Rule
    public JUnitRuleMockery2 context = 
        JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);

    @One
    @Mock
    private Collaborator collaborator;

    @ClassUnderTest
    private Collaborating collaborating;

    @Before
    public void setUp() throws Exception {
	    collaborating = (Collaborating) context.getClassUnderTest();
    }

    @Test
    public void autoWires_and_collaborates() {
        assertThat(collaborating, is(not(nullValue())));
	    assertThat(collaborating.collaborator, is(not(nullValue())));
        collaborating.collaborateWithCollaborator();
    }
}

Note that this class has dependencies on a number of other libraries:

  • org.junit:junit:4.10
  • org.jmock:jmock:2.5.1
  • org.jmock:jmock-junit4:2.5.1
  • org.jmock:jmock-legacy:2.5.1
  • org.hamcrest:hamcrest-core:1.3
  • org.picocontainer:picocontainer:2.14.1

Note that the @Mock support has been backported from JMock 2.6 (not released as of this writing).

For further discussion on some of these features, see this blog post.

DbUnitRule

Easily test database code using a JUnit rule that integrates with DbUnit a JSON dataset.

For example:

public class DbUnitRuleTest {

    @Rule
    public DbUnitRule dbUnit = new DbUnitRule(
        DbUnitRuleTest.class,
		jdbcDriver.class, "jdbc:hsqldb:file:src/test/resources/testdb", "SA", "");

    @Ddl("customer.ddl")
    @JsonData("customer.json")
    @Test
    public void update_lastName() throws Exception { ... }
}

where customer.ddl provides the schema, and customer.json is the initial data set to populate the table with

Note that this class has dependencies on a number of other libraries:

  • org.junit:junit:4.10
  • org.dbunit:dbunit:2.4.8
  • com.google.guava:guava:12.0.1
  • org.codehaus.jackson:jackson-core-asl:1.9.8
  • org.codehaus.jackson:jackson-mapper-asl:1.9.8
  • org.slf4j:slf4j-api:1.6.6
  • org.slf4j:slf4j-nop:1.6.6

For further discussion, see this blog post.