I have setter functions that check their input data by passing it to a validation function before changing private variables. If the data is invalid, the getter functions throw an exception. How can I unit the setter functions and the data validation functions without repeating all of my input test cases for each of them? Both functions validate the data in the same way.
EDIT: Sorry, I should have included code to begin with.
setterFunction(String value) {
if (valueValid()) {
// Update member variable
} else {
throw new RuntimeException("Invalid Data");
}
}
Both valueValid() and setterFunction fail on the same values, so I don’t want to have duplicate test code.
I think it would be wise to test the validator with all test cases, and then test that the setter and getter call the validator correctly using a simple case or two.
This way if something fails – you also have a better understanding of where the bug is (the validator or setters/getters)