com.midwinter.junit
Class GetterSetterTester

java.lang.Object
  extended by com.midwinter.junit.GetterSetterTester

public class GetterSetterTester
extends java.lang.Object

Automates JUnit testing of simple getter/setter methods.

It may be used in exclusive or inclusive mode. In exclusive mode, which is the default, all JavaBeans properties (getter/setter method pairs with matching names) are tested unless they are excluded beforehand. For example:

 MyClass objectToTest = new MyClass();
 GetterSetterTester gst = new GetterSetterTester(objectToTest);
 gst.exclude("complexProperty");
 gst.exclude("anotherProperty");
 gst.test();
 

In inclusive mode, only properties that are explicitly listed are tested. For example:

 new GetterSetterTester(new MyClass()).
     include("aSimpleProperty").
     include("secondProperty").
     test();
 

The second example also illustrates how to call this class in as terse a way as possible.

The following property types are supported:

Properties whose types are classes declared final are not supported; neither are non-primitive, non-interface properties if you don't have cglib.

Copyright (c) 2005, Steven Grimm.
This software may be used for any purpose, commercial or noncommercial, so long as this copyright notice is retained. If you make improvements to the code, you're encouraged (but not required) to send them to me so I can make them available to others. For updates, please check here.

Version:
1.0 (2005/11/08).
Author:
Steven Grimm koreth@midwinter.com

Constructor Summary
GetterSetterTester(java.lang.Object obj)
          Constructs a new getter/setter tester to test objects of a particular class.
 
Method Summary
 GetterSetterTester exclude(java.lang.String field)
          Adds a field to the list of excluded fields.
 GetterSetterTester include(java.lang.String field)
          Adds a field to the list of tested fields.
 GetterSetterTester setVerbose(boolean verbose)
          Sets the verbosity flag.
 void test()
          Walks through the methods in the class looking for getters and setters that are on our include list (if any) and are not on our exclude list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GetterSetterTester

public GetterSetterTester(java.lang.Object obj)
Constructs a new getter/setter tester to test objects of a particular class.

Parameters:
obj - Object to test.
Method Detail

include

public GetterSetterTester include(java.lang.String field)
Adds a field to the list of tested fields. If this method is called, the tester will not attempt to list all the getters and setters on the object under test, and will instead simply test all the fields in the include list.

Parameters:
field - Field name whose getter/setter should be tested.
Returns:
This object, so include calls can be chained together.

exclude

public GetterSetterTester exclude(java.lang.String field)
Adds a field to the list of excluded fields.

Parameters:
field - Field name to exclude from testing.
Returns:
This object, so exclude calls can be chained together.

setVerbose

public GetterSetterTester setVerbose(boolean verbose)
Sets the verbosity flag.


test

public void test()
          throws java.lang.reflect.InvocationTargetException,
                 java.lang.IllegalArgumentException,
                 java.lang.IllegalAccessException,
                 java.lang.SecurityException,
                 java.lang.ClassNotFoundException,
                 java.lang.NoSuchMethodException,
                 java.lang.InstantiationException
Walks through the methods in the class looking for getters and setters that are on our include list (if any) and are not on our exclude list.

Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.ClassNotFoundException
java.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.InstantiationException
java.lang.reflect.InvocationTargetException


Copyright © 2007. All Rights Reserved.