public class EmailValidator implements Validator {
public void validate(FacesContext context, UIComponent arg1, Object value)
throws ValidatorException {
String email = (String) value;
if (!email.contains("@")) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary(" Email is not valid.");
message.setDetail(" Email is not valid.");
context.addMessage("userForm:Email", message);
throw new ValidatorException(message);
}
}
}
public class EmailValidator implements Validator { public void validate(FacesContext context, UIComponent arg1, Object value)
Share
What do you wanna test here at all? If
if (!email.contains("@"))is working? If you want to test this from a user perspective, you want to have an integration or GUI test with something like Selenium.If you want to have a unit test for your email validation, I would do this in the class:
and then write a test class like this:
EDIT
Well, you could write a test for that method by providing so called Mock objects. Then you just expect an exception to occur when the provided email address is invalid:
The FacesContextMock is just a mockup that implements the FacesContext methods without doing anything:
That said, I don’t think you have to write test cases for everything just to get close to 100% code coverage. I think what should be tested and how much code coverage one needs is discussed in great detail over at https://sqa.stackexchange.com/