GetSetTester

Overview

GetSetTester automatically tests get and set methods for properties, either one at a time as specified or the whole class. To test all class properties, it uses reflection.

When get/set/is methods do not exist on the class, it will either throw an exception or track them and return the set of them, based on the value passed for "failOnNoMethod".

Additionally, if also passed a set of expected missing methods, it will verify any actual missing methods found (e.g. missing a set method) match the specified expected missing methods.

Classes

GetSetTester

Use one of the testBean methods to test all properties of a class.

Use one of the testProperty methods to test only a specified property.

Exceptions

Custom exceptions are thrown for specific issues, such as failing a property validation.

Usage

In the test class, create a test method, such as:

@Test
public void testGetSet() throws Exception {
    TheClassToTest bean = new TheClassToTest();
    Set<String> expectedMissingMethods = new HashSet<String>();
    expectedMissingMethods.add("setPerson");
    expectedMissingMethods.add("isBirthday");

    GetSetTester tester = new GetSetTester();
    tester.testBean(bean, false, expectedMissingMethods);
}

In the above example, "false" states to not fail on missing methods, allowing the validation of expectedMissingMethods to occur.

If the class has all get/set/is methods for properties, then pass true to have it fail on missing methods.

@Test
public void testGetSet() throws Exception {
    TheClassToTest bean = new TheClassToTest();

    GetSetTester tester = new GetSetTester();
    tester.testBean(bean, true);
}

Refer to the JavaDoc for more details on the GetSetTester class. Also review the GetSetTesterTest class for other usage examples.