简体   繁体   English

无法在基本类型 boolean 上调用 isEqualTo(boolean)

[英]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. Boolean isValid 是需要测试的方法。

 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.
    }
 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 . isEqualTo不是以assertEquals开头的——它以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.由于缺少括号,您现在尝试在原语 boolean 上调用.isEqualTo It's not an object, primitiveExpression.anything doesn't work in java.它不是 object, primitiveExpression.anything在 java 中不起作用。

You presumably want either:您可能想要:

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

or more likely:或更可能:

assertTrue(v.isValid(email));

//you can try is it with assertTrue and pass the value type return by isValid() //你可以用assertTrue试试它,并通过isValid()传递值类型返回

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM