-
Notifications
You must be signed in to change notification settings - Fork 41
Tutorial 1. Getting Started
Osman Shoukry edited this page Mar 31, 2020
·
7 revisions
You will need the following
- JDK 1.5+
- OpenPojo jar in your classpath - see http://bit.ly/openpojo-latest or Maven
- Optional - ASM 5.03+, will only be needed if you plan to test abstract classes - http://asm.ow2.org/.
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.