Fork me on GitHub

beanunit by IvoNet

A unit test extention for making unit testing of POJO's / DTO's / Beans and immutable beans easy.

Every self respecting java programmer hates testing of pojo's, but it should / must be done. Beanunit makes it very easy to do an almost complete test of these simple objects with only one line of code. Just check out the code and look at the unit tests for a better explanation.

Dependencies

It has a dependency on junit.

Install

    <dependencies>
        ...
        <dependency>
            <groupId>nl.ivonet</groupId>
            <artifactId>beanunit</artifactId>
            <version>2.0</version>
            <scope>test</scope>
        </dependency>
        ...
    </dependencies>
    

Usage examples

Example 1: Simple POJO called Person.

Tip: look at all the possible parameters fot this method. You can also exclude certain properties.
    @Test
    public void testAssertPojo() throws Exception {
        PojoContractAsserter.assertBean(Person.class);
    }
    

Example 2: Bean created by constructor and immutable after construction

    @Test
    public void testAssertPojo() throws Exception {
        ConstructedBeanAsserter.assertBean(Person.class);
    }
    

Example 3: Immutable bean created by a Builder

    @Test
    public void testAssertPojo() throws Exception {
        BuilderBeanAsserter.assertBean(Person.class);
    }
    

Example 4: Register types for usage by beanunit


If you have special Objects needed to construct your bean that can not be created by a default constructor you need to register them so beanunit can use them in its tests.
    @Before
    public void setUp() throws Exception {
        registerTypeAndDefaultArgument(AddressDto.class, new AddressDto.Builder().setCity("c").build());
        registerTypeAndDefaultArgument(BigDecimal.class, new BigDecimal(42));
    }

    @After
    public void tearDown() throws Exception {
        resetToDefaultTypes();
    }
    

Example 5: Create one of your beans for further testing


The createObject can be a static import from any of the three Asserters.
    @Test
    public void testCreateObject() throws Exception {
        final Employee employee = createObject(Employee.class);
        assertNotNull(employee);
        assertEquals("Employee{id='String'}", employee.toString());
    }
    

License

    Copyright 2011 Ivo Woltring

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    

Authors

Ivo Woltring

Contact


Twitter: @ivonet

Revision History

    Version 2.0:
        - Major version because the interface has been changed
        - Now only the assertBean methods are public
        - Better names for the Asserter classes.
        - Easier to use as the above examples will demonstrate
        - added the createObject method as a convenience for unit testers that want to
          create an object the easy way for further testing of their own.
        - Some code cleanup.
        - More typos
    Version 1.0:
        - Fully working version
        - some textual improvements on fails
        - added the assertBean method as a replacement for all the other methods
        - more unit tests
    Version 0.3, 0.4 and 0.6
        - trial versions (fully working of course)
        - first releases to maven central
    

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/IvoNet/beanunit