我需要一个方法,我可以在junit assertTrue() 方法中调用它比较两个布尔值来检查它们是否相等,返回一个布尔值 . 例如,像这样:
assertTrue()
boolean isEqual = Boolean.equals(bool1, bool2);
如果它们不相等则应返回false,如果不相等则返回true . 我已经检查了布尔类,但唯一接近的是 Boolean.compare() ,它返回一个我无法使用的int值 .
Boolean.compare()
== 运算符与布尔值一起使用 .
==
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))); } }
2 回答
==
运算符与布尔值一起使用 .(括号是不必要的,但有助于使其更容易阅读 . )