Skip to content

Commit

Permalink
Make isNotEqualTo(boolean) to pass when actual is null
Browse files Browse the repository at this point in the history
Fix #3341
  • Loading branch information
biergit authored and joel-costigliola committed May 4, 2024
1 parent e84ee3a commit ba02181
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 48 deletions.
16 changes: 3 additions & 13 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,10 @@ 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.
* @throws AssertionError if the actual value is not equal to the expected one.
*/
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 +58,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 @@ -12,8 +12,10 @@
*/
package org.assertj.core.internal;

import static org.assertj.core.test.TestData.someInfo;
import static org.mockito.Mockito.spy;

import org.assertj.core.api.AssertionInfo;
import org.junit.jupiter.api.BeforeEach;

/**
Expand All @@ -25,6 +27,8 @@
*/
public class BooleansBaseTest {

protected static final AssertionInfo INFO = someInfo();

protected Failures failures;
protected Booleans booleans;

Expand All @@ -35,4 +39,4 @@ public void setUp() {
booleans.failures = failures;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
*/
package org.assertj.core.internal.booleans;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
import static java.lang.String.format;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.AssertionInfo;
Expand All @@ -35,24 +34,33 @@
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());
void should_fail_if_actual_is_null_since_the_other_argument_cannot_be_null() {
// GIVEN
Boolean actual = null;
// WHEN
AssertionError assertionError = expectAssertionError(() -> booleans.assertEqual(INFO, actual, true));
// THEN
then(assertionError).hasMessage(format("%n" +
"expected: true%n" +
" but was: null"));
}

@Test
void should_pass_if_booleans_are_equal() {
booleans.assertEqual(someInfo(), TRUE, true);
booleans.assertEqual(INFO, true, true);
booleans.assertEqual(INFO, TRUE, true);
booleans.assertEqual(INFO, FALSE, false);
booleans.assertEqual(INFO, false, false);
}

@Test
void should_fail_if_booleans_are_not_equal() {
AssertionInfo info = someInfo();
// GIVEN
boolean actual = TRUE;
boolean expected = false;

Throwable error = catchThrowable(() -> booleans.assertEqual(info, TRUE, expected));

assertThat(error).isInstanceOf(AssertionError.class);
verify(failures).failure(info, shouldBeEqual(TRUE, expected, info.representation()));
// WHEN
expectAssertionError(() -> booleans.assertEqual(INFO, actual, expected));
// THEN
verify(failures).failure(INFO, shouldBeEqual(actual, expected, INFO.representation()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
*/
package org.assertj.core.internal.booleans;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.error.ShouldNotBeEqual.shouldNotBeEqual;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.AssertionInfo;
Expand All @@ -35,23 +32,33 @@
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_since_the_other_argument_cannot_be_null() {
booleans.assertNotEqual(INFO, null, false);
booleans.assertNotEqual(INFO, null, FALSE);
booleans.assertNotEqual(INFO, null, true);
booleans.assertNotEqual(INFO, null, TRUE);
}

@Test
void should_pass_if_bytes_are_not_equal() {
booleans.assertNotEqual(someInfo(), TRUE, false);
void should_pass_if_booleans_are_not_equal() {
booleans.assertNotEqual(INFO, true, false);
booleans.assertNotEqual(INFO, true, FALSE);
booleans.assertNotEqual(INFO, TRUE, false);
booleans.assertNotEqual(INFO, TRUE, FALSE);
booleans.assertNotEqual(INFO, false, true);
booleans.assertNotEqual(INFO, false, TRUE);
booleans.assertNotEqual(INFO, FALSE, true);
booleans.assertNotEqual(INFO, FALSE, TRUE);
}

@Test
void should_fail_if_bytes_are_equal() {
AssertionInfo info = someInfo();

Throwable error = catchThrowable(() -> booleans.assertNotEqual(info, TRUE, true));

assertThat(error).isInstanceOf(AssertionError.class);
verify(failures).failure(info, shouldNotBeEqual(TRUE, true));
void should_fail_if_booleans_are_equal() {
// GIVEN
boolean actual = TRUE;
boolean expected = true;
// WHEN
expectAssertionError(() -> booleans.assertNotEqual(INFO, actual, expected));
// THEN
verify(failures).failure(INFO, shouldNotBeEqual(actual, expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public final class TestData {

private static final WritableAssertionInfo ASSERTION_INFO = new WritableAssertionInfo();
private static final WritableAssertionInfo ASSERTION_INFO_AS_HEX = new WritableAssertionInfo();
private static final TextDescription DESCRIPTION = new TextDescription(
"who's the more foolish: the fool, or the fool who follows him?");
private static final TextDescription DESCRIPTION = new TextDescription("who's the more foolish: the fool, or the fool who follows him?");
private static final Index INDEX = atIndex(0);
private static final Pattern MATCH_ANYTHING = Pattern.compile(".*");

Expand Down

0 comments on commit ba02181

Please sign in to comment.