Kshitij
Kshitij

Reputation: 11

how i will write test case for this method it is in JSF?

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);

        }
    }

}

Upvotes: 1

Views: 2679

Answers (1)

Tim Büthe
Tim Büthe

Reputation: 63814

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:

public class EmailValidator implements Validator {
    public void validate(FacesContext context, UIComponent arg1, Object value)
            throws ValidatorException {
        String email = (String) value;
        if (!isValidEmail(email)) {
            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);

        }
    }

   boolean isValidEmail(String emailAddress){
     // do your tests here...
   }
}

and then write a test class like this:

public class EmailValidatorTest {

  @Test
  public testIsValidEmail(){

    EmailValidator validator = new EmailValidator();

    Assert.assertTrue(validator.isValidEmail("[email protected]");
    Assert.assertFalse(validator.isValidEmail("aaa");
    // more assertions here...

  }
}

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:

public class EmailValidatorTest{

  @Test
  public void testValidateWithValidAddresses(){
    FacesContextMock facesContextMock = new FacesContextMock();
    EmailValidator validator = new EmailValidator();
    validator.validate(facesContextMock, null, "[email protected]");
  }

  @Test(expected=ValidatorException.class)
  public void testValidateWithInvalidAddresses(){
    FacesContextMock facesContextMock = new FacesContextMock();
    EmailValidator validator = new EmailValidator();
    validator.validate(facesContextMock, null, "foo");
  }   
}

The FacesContextMock is just a mockup that implements the FacesContext methods without doing anything:

public class FacesContextMock extends FacesContext{
  @Override
  public Application getApplication(){
    return null; 
  }

  // all the abstract methods implemented here, just doing nothing
}

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/

Upvotes: 5

Related Questions