Git Product home page Git Product logo

java-testsupport's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.