POJOUnit

Plain Old Java Object Unit Test Framework

License

License

GroupId

GroupId

com.github.almex
ArtifactId

ArtifactId

pojo-unit
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

POJOUnit
Plain Old Java Object Unit Test Framework
Project URL

Project URL

https://github.com/almex/POJOUnit
Source Code Management

Source Code Management

https://github.com/almex/POJOUnit.git

Download pojo-unit

How to add to project

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

Dependencies

compile (7)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.5
org.apache.logging.log4j : log4j-slf4j-impl jar 2.5
org.apache.logging.log4j : log4j-core jar 2.5
org.apache.logging.log4j : log4j-api jar 2.5
org.apache.logging.log4j : log4j-1.2-api jar 2.5
junit : junit jar 4.12
org.hamcrest : hamcrest-junit jar 2.0.0.0

Project Modules

There are no modules declared in this project.

POJOUnit

Build Status Coverage Status Maven Central

This is a simple Java Framework to generify unit testing of POJO (Plain Old Java Objects) and JavaBean. To do so we implemented a generic class by using JUnit Theories (read its Javadoc and the documentation) making it easy to test combination of different DataPoint.

Combined with Harmcrest framework, we can describe easily some assumption on the behaviour of some methods. By reading Javadoc of the Object class for methods hashCode(), equals(Object) and toString() we were able to retranscript that documentation in Java code.

Here is a snipped of the Theory used to test the hashCode() method :

    /**
     * Whenever it is invoked on the same object more than once during an
     * execution of a Java application, the hashCode method must consistently
     * return the same integer, provided no information used in equals
     * comparisons on the object is modified. This integer need not remain
     * consistent from one execution of an application to another execution of
     * the same application.
     *
     * @param x object to test
     */
    @Theory
    public void theoryHashCodeIsSelfConsistent(final Object x) {
        assumeThat(x, is(notNullValue()));

        final int theSame = x.hashCode();

        for (int i = 0; i < NB_CONSISTENT_LOOP; i++) {
            assertThat(x.hashCode(), is(equalTo(theSame)));
        }
    }

Reading the Javadoc of that method (copied from the Object Javadoc) and comparing with the Java code, those two should sound identical.

Usage

Assuming that you have a POJO named Person and that you want to test methods hashCode(), equals(Object) and toString()

public class Person {

    protected Long id;

    protected String firstName;

    protected String lastName;

    protected Address address;

    public Long getId() {
        return id;
    }

    // ... Removed for readability
}

Then the test would be :

public class PersonTest extends AbstractObjectTest {

    @DataPoint
    public static Person DATA_POINT1;

    // ... Removed for readability

    @Before
    @Override
    public void setUp() throws Exception {
        DATA_POINT1 = new Person();
        setIdFor(DATA_POINT1, 1L);
        DATA_POINT1.setFirstName("foo");
        DATA_POINT1.setLastName("bar");

        DATA_POINT3 = new Person();
        setIdFor(DATA_POINT3, 2L);
        DATA_POINT3.setFirstName("foo");
        DATA_POINT3.setLastName("bar");
    }
}

Note here that we have created 3 DataPoint :

  • The first one as a common instance of that type
  • The second one is null because we want to test against null references of type Person
  • The third one has the same content as the first one but with another id

As an extra, AbstractObjectTest contains also two DataPoint :

  • An instance of Object making some possible comparison with at least one different type that the one you are testing.
  • A null reference to Object to never forget to test null value.

So, running this test will try any combination of those 5 DataPoint and run each Theory against them.

3rd Party Framework

  • JUnit : as the base framework for unit-tests
  • Hamcrest : for its DSL

Versions

Version
1.1.0
1.0.0