Search code examples
javajunitbooleancompare

How to check if two boolean values are equal?


I need a method which I can call within the junit assertTrue() method which compares two booleans to check if they are equal, returning a boolean value. For example, something like this:

boolean isEqual = Boolean.equals(bool1, bool2);

which should return false if they are not equal, or true if they are. I've checked out the Boolean class but the only one that comes close is Boolean.compare() which returns an int value, which I can't use.


Solution

  • The == operator works with booleans.

    boolean isEqual = (bool1 == bool2);
    

    (The parentheses are unnecessary, but help make it easier to read.)