Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve equality checks for Boolean null values #3343

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 2 additions & 10 deletions assertj-core/src/main/java/org/assertj/core/internal/Booleans.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ public static Booleans instance() {
* @param info contains information about the assertion.
* @param actual the actual value.
* @param expected the expected value.
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if the actual value is not equal to the expected one. This method will throw a
* {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the expected and actual values are not
* equal.
*/
public void assertEqual(AssertionInfo info, Boolean actual, boolean expected) {
assertNotNull(info, actual);
if (actual == expected) return;
if (actual != null && actual == expected) return;
throw failures.failure(info, shouldBeEqual(actual, expected, info.representation()));
}

Expand All @@ -62,16 +60,10 @@ public void assertEqual(AssertionInfo info, Boolean actual, boolean expected) {
* @param info contains information about the assertion.
* @param actual the actual value.
* @param other the value to compare the actual value to.
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if the actual value is equal to the other one.
*/
public void assertNotEqual(AssertionInfo info, Boolean actual, boolean other) {
assertNotNull(info, actual);
if (actual != other) return;
if (actual == null || actual != other) return;
throw failures.failure(info, shouldNotBeEqual(actual, other));
}

private static void assertNotNull(AssertionInfo info, Boolean actual) {
Objects.instance().assertNotNull(info, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,21 @@ class Booleans_assertEqual_Test extends BooleansBaseTest {

@Test
void should_fail_if_actual_is_null() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> booleans.assertEqual(someInfo(), null, true))
.withMessage(actualIsNull());
AssertionInfo info = someInfo();
boolean expectedFalse = false;
Boolean actual = null;

Throwable errorOnExpectingFalse = catchThrowable(() -> booleans.assertEqual(someInfo(), actual, expectedFalse));

assertThat(errorOnExpectingFalse).isInstanceOf(AssertionError.class);
verify(failures).failure(info, shouldBeEqual(actual, expectedFalse, info.representation()));

boolean expectedTrue = true;

Throwable errorOnExpectingTrue = catchThrowable(() -> booleans.assertEqual(someInfo(), actual, expectedTrue));

assertThat(errorOnExpectingTrue).isInstanceOf(AssertionError.class);
verify(failures).failure(info, shouldBeEqual(actual, expectedTrue, info.representation()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
class Booleans_assertNotEqual_Test extends BooleansBaseTest {

@Test
void should_fail_if_actual_is_null() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> booleans.assertNotEqual(someInfo(), null, false))
.withMessage(actualIsNull());
void should_pass_if_actual_is_null() {
booleans.assertNotEqual(someInfo(), null, false);
booleans.assertNotEqual(someInfo(), null, true);
}

@Test
Expand Down