Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

java - Cannot invoke isEqualTo(boolean) on the primitive type boolean

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 = "[email protected]";
        assertEquals(v.isValid(email).isEqualTo(true)); //this line gives the error.
    }
question from:https://stackoverflow.com/questions/66045253/cannot-invoke-isequaltoboolean-on-the-primitive-type-boolean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...