Quick Guide to AssertJ - dn0000001/test-automation GitHub Wiki

Cause a failure on purpose

AssertJUtil.fail("Data not provided");

Common assertions to ensure valid value

AssertJUtil.assertThat("aaa")
  .as("Is Not Blank").isNotBlank()
  .as("Is Not Empty").isNotEmpty();

Regex examples

AssertJUtil.assertThat("anything")
  .as("wildcard test").matches(".*")
  .as("non-matching regex").doesNotMatch("aaa.*");

String parse to date for comparison

AssertJUtil.assertThat("01/01/2021")
  .as("Date Equal To")
  .is(AssertJCondition.dateEqualTo("01/01/2021", "MM/dd/yyyy"));

BigDecimal example

BigDecimal actual = new BigDecimal("5");
BigDecimal expected = new BigDecimal("5.00");
AssertJUtil.assertThat(actual).as("BigDecimal Equal To").isEqualByComparingTo(expected);

Ignoring expected objects null fields

Integer expectedInteger = 10;
AssertJUtil.assertThat(actualInteger).as("Integer match").is(AssertJCondition.isEqualToIgnoringNullFields(expectedInteger));

AssertJUtil.assertThat(actualObject).as("Object match").isEqualToIgnoringNullFields(expectedObject);

Aggregation of failures

// For aggregation
CustomSoftAssertions softly = new CustomSoftAssertions();

// Perform any assertions to be aggregated
softly.assertThat("").as("value is null").isNull();
softly.assertThat(100).as("another check").isEqualTo(99);

// Trigger any assertion failures when complete
softly.assertAll();

When there is no "not" assertion that you want to do, then the following is a workaround

CustomSoftAssertions softly = new CustomSoftAssertions();
softly.assertExpectedFailure("Assertion is supposed to fail", () -> AssertJUtil.assertThat(5000).isEqualTo(400));

// Trigger any assertion failures when complete
softly.assertAll();

AssertJUtil.assertExpectedFailure("Should not stop execution", () -> AssertJUtil.assertThat(25).isEqualTo(24));