nl.ivonet.beanunit
Class ConstructorImmutableBeanAsserter

java.lang.Object
  extended by nl.ivonet.beanunit.Asserter
      extended by nl.ivonet.beanunit.ConstructorImmutableBeanAsserter

public class ConstructorImmutableBeanAsserter
extends Asserter

Test utility for testing beans that are immutable after construction. Complete construction must be done by a constructor for the tests in this class to work.

Construction of the immutable object is done by reflection and providing default types with values.

This class tries to assert that behavior of the bean after construction is valid. There should be no write methods in the class under test. All attributes should have read methods. All read methods (getters) should return the default value provided for the return type.

Attributes can be excluded by providing their string representation as a parameter to the method.

Author:
Ivo Woltring

Method Summary
static
<T> void
assertEqualsHashCode(java.lang.Class<T> classUnderTest)
          Tests all the flows of the overridden equals and hashCode methods of a class.
static
<T> void
assertGettersOnConstructorImmutableObject(java.lang.Class<T> classUnderTest, java.lang.String... excludedProperties)
          Asserts that the Getters return the default value for the return type of the getter.
 
Methods inherited from class nl.ivonet.beanunit.Asserter
deregisterType, registerTypeAndDefaultArgument, resetToDefaultTypes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertGettersOnConstructorImmutableObject

public static <T> void assertGettersOnConstructorImmutableObject(java.lang.Class<T> classUnderTest,
                                                                 java.lang.String... excludedProperties)
Asserts that the Getters return the default value for the return type of the getter. Asserts that the object can be created by providing default values for the wanted types to the constructor. Asserts that the object does not have write methods (= immutable) for the member variables. Tries to do all this for all available constructors.

Type Parameters:
T - the type of the class under test.
Parameters:
classUnderTest - the Class to test
excludedProperties - property to exclude from testing if some of the rules are not nicely upheld :-)

assertEqualsHashCode

public static <T> void assertEqualsHashCode(java.lang.Class<T> classUnderTest)
Tests all the flows of the overridden equals and hashCode methods of a class.

- it ensures that if A == B, B == A also - it tests the equals and hasCode methods for all the separate writable properties - it only tests if the equals method and the hashCode method are both overridden - it fails of only one is overridden - The both methods need to be overridden at the same level (not java.lang.Object)

You can exclude properties by adding them to the method argument list.

Type Parameters:
T - the type of the class to test
Parameters:
classUnderTest - the implementation.class


Copyright © 2011 IvoNet. All Rights Reserved.