Skip to content

Tutorial 1. Getting Started

Osman Shoukry edited this page Mar 31, 2020 · 7 revisions

Getting Started

Setting up

You will need the following

  1. JDK 1.5+
  2. OpenPojo jar in your classpath - see http://bit.ly/openpojo-latest or Maven
  3. Optional - ASM 5.03+, will only be needed if you plan to test abstract classes - http://asm.ow2.org/.

Simple Test

Suppose you have the following class Person that you would like to test its getters and setters.

public class Person {
  private String name;

  public void setName(String name) {
    this.name = name;
  }    

  public String getName() {
    return name;
  }
}

To test it you'll need the following JUnit test

public class PersonTest {
  @Test
  public void validateSettersAndGetters() {
    PojoClass personPojo = PojoClassFactory.getPojoClass(Person.class);
    Validator validator = ValidatorBuilder.create()
                            // Lets make sure that we have a getter and a setter for every field defined.
                            .with(new GetterMustExistRule())
                            .with(new SetterMustExistRule())
                            // Lets also validate that they are behaving as expected
                            .with(new SetterTester())
                            .with(new GetterTester())
                            .build();

    // Start the Test
    validator.validate(personPojo);
  }
}

Now whenever you add a field in class Person or remove a field, the this test will ensure that you've followed the two basic rules of adding a getter and setter, as well as their behavior is preserved.

Clone this wiki locally