Skip to content

Soft assertions fail in confusing ways with null values #4213

@doublep

Description

@doublep

Describe the bug

See the test case.

  • assertj core version: 3.27.7
  • java version: 21.0.9
  • test framework version: -
  • os (if relevant): -

Test case reproducing the bug

Add a test case showing the bug that we can run

        try (var softly = new AutoCloseableSoftAssertions ()) {
            Object  value = null;  // in real tests this would be a result of invoking some method, for example
            softly.assertThat (value).isNotNull ().extracting ("foo").isEqualTo ("bar");
        }

expecting this to produce exactly one failure like this (got this by removing .extracting ("foo").isEqualTo ("bar")):

Exception in thread "main" org.assertj.core.api.SoftAssertionError: 
The following assertion failed:
1) 
Expecting actual not to be null
at Foo.main(Foo.java:15)

        at org.assertj.core.error.AssertionErrorCreator.multipleSoftAssertionsError(AssertionErrorCreator.java:102)
        at org.assertj.core.api.AbstractSoftAssertions.assertAll(AbstractSoftAssertions.java:41)
        at org.assertj.core.api.AbstractSoftAssertions.assertAll(AbstractSoftAssertions.java:46)
        at org.assertj.core.api.AutoCloseableSoftAssertionsProvider.close(AutoCloseableSoftAssertionsProvider.java:34)
        at Foo.main(Foo.java:16)

Instead it dies with an exception, meaning that it doesn't work "softly" anymore, and the output is also pretty confusing:

Exception in thread "main" java.lang.IllegalArgumentException: The object to extract property/field from should not be null
        at org.assertj.core.util.Preconditions.checkArgument(Preconditions.java:134)
        at org.assertj.core.util.introspection.PropertyOrFieldSupport.getValueOf(PropertyOrFieldSupport.java:53)
        at org.assertj.core.extractor.ByNameSingleExtractor.apply(ByNameSingleExtractor.java:32)
        at org.assertj.core.api.AbstractAssert.extracting(AbstractAssert.java:1266)
        at org.assertj.core.api.AbstractObjectAssert.extracting(AbstractObjectAssert.java:806)
        at org.assertj.core.api.ObjectAssert$ByteBuddy$UbgnH5po.extracting$accessor$oMeG1udP(Unknown Source)
        at org.assertj.core.api.ObjectAssert$ByteBuddy$UbgnH5po$AssertJ$SoftProxies$rsSMPYj6.call(Unknown Source)
        at org.assertj.core.api.ProxifyMethodChangingTheObjectUnderTest.intercept(ProxifyMethodChangingTheObjectUnderTest.java:82)
        at org.assertj.core.api.ObjectAssert$ByteBuddy$UbgnH5po.extracting(Unknown Source)
        at Foo.main(Foo.java:15)
        Suppressed: org.assertj.core.api.SoftAssertionError: 
The following 2 assertions failed:
1) 
Expecting actual not to be null
at Foo.main(Foo.java:15)
2) 
Expecting actual not to be null
at Foo.main(Foo.java:15)

                at org.assertj.core.error.AssertionErrorCreator.multipleSoftAssertionsError(AssertionErrorCreator.java:102)
                at org.assertj.core.api.AbstractSoftAssertions.assertAll(AbstractSoftAssertions.java:41)
                at org.assertj.core.api.AbstractSoftAssertions.assertAll(AbstractSoftAssertions.java:46)
                at org.assertj.core.api.AutoCloseableSoftAssertionsProvider.close(AutoCloseableSoftAssertionsProvider.java:34)
                at Foo.main(Foo.java:13)

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions