我需要可以在junit assertTrue()
方法中调用的方法,该方法比较两个布尔值以检查它们是否相等,并返回一个布尔值。例如,如下所示:
boolean isEqual = Boolean.equals(bool1, bool2);
如果不相等,则返回false;如果不相等,则返回true。我已经签出了Boolean类,但是唯一接近的是Boolean.compare()
,它返回一个int值,我不能使用它。
[==
运算符与布尔值一起使用。
boolean isEqual = (bool1 == bool2);
(括号是不必要的,但有助于使其更易于阅读。)
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class BooleanEqualityTest {
@Test
public void equalBooleans() {
boolean boolVar1 = true;
boolean boolVar2 = true;
assertTrue(boolVar1 == boolVar2);
assertThat(boolVar1, is(equalTo(boolVar2)));
}
}
您可以使用逻辑表达式
boolean isEqual = bool1 && bool2;