Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:58:00+00:00 2026-06-08T08:58:00+00:00

public class EmailValidator implements Validator { public void validate(FacesContext context, UIComponent arg1, Object value)

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

        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:58:01+00:00Added an answer on June 8, 2026 at 8:58 am

    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("foo@bar.com");
        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, "foo@bar.com");
      }
    
      @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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class MyEncrypt { public void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
public class Test { public static void main(String[] args) throws Exception { String s1
public class ReportView extends JFrame { Connection con=null; void showReport() throws SQLException, ClassNotFoundException, JRException
public class Main { public static void main(String[] args) throws InterruptedException { ClassA a
public class readBin { public static void main(String[] args) throws IOException{ long time1 =
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.