-2

This code needs to be tested but I'm not able to resolve the error. Boolean isValid is the method which needs to be tested.

 public boolean isValid(String email) 
    { 
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+ 
                            "[a-zA-Z0-9_+&*-]+)*@" + 
                            "(?:[a-zA-Z0-9-]+\\.)+[a-z" + 
                            "A-Z]{2,7}$"; 
                              
        Pattern pat = Pattern.compile(emailRegex); 
        if (email == null) 
            return false; 
        return pat.matcher(email).matches(); 
    } 

Getting error in writing test case: Cannot invoke isEqualTo(boolean) on the primitive type boolean
@Test
    public void isValidTest() {
        Validation v = new Validation();
        String email = "bhavya@gmail.com";
        assertEquals(v.isValid(email).isEqualTo(true)); //this line gives the error.
    }
Bhavya
  • 109
  • 2
  • 6

2 Answers2

2
assertEquals(v.isValid(email).isEqualTo(true);

This line is wrong in so many ways.

  1. The parentheses don't even line up.
  2. The isEqualTo thing does not start with assertEquals - it starts with assertThat. You're mixing two completely different ways to write tests.
  3. Because of the missing parens, you are now attempting to invoke .isEqualTo on a primitive boolean. It's not an object, primitiveExpression.anything doesn't work in java.

You presumably want either:

assertThat(v.isValid(email)).isEqualTo(true);

or more likely:

assertTrue(v.isValid(email));
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
1

//you can try is it with assertTrue and pass the value type return by isValid()

@Test
public void isValidTest() {
   Validation v = new Validation();
   String email = "bhavya@gmail.com";
   Boolean value = v.isValid(email);
   assertTrue(value); 
}