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

NPE with extracting(Function... extractors) #3375

Open
stanio opened this issue Feb 23, 2024 · 3 comments
Open

NPE with extracting(Function... extractors) #3375

stanio opened this issue Feb 23, 2024 · 3 comments
Labels
type: improvement A general improvement

Comments

@stanio
Copy link

stanio commented Feb 23, 2024

Assertions using extracting(Function... extractors) over an actual null object/reference fail with NullPointerException:

java.lang.NullPointerException
	at org.assertj.core.api.AbstractObjectAssert.lambda$extractingForProxy$0(AbstractObjectAssert.java:926)
	at java.base/...
	at org.assertj.core.api.AbstractObjectAssert.extractingForProxy(AbstractObjectAssert.java:927)
	at org.assertj.core.api.AbstractObjectAssert.extracting(AbstractObjectAssert.java:917)
  • assertj core version: 3.25.3
  • java version: 8, 11, 17, 21
  • test framework version: junit5

Test case reproducing the bug

import static org.assertj.core.api.Assertions.assertThat;

import java.awt.geom.Point2D;

import org.junit.jupiter.api.Test;

    @Test
    void verifyCoordinates() {
        Point2D point = null; //new Point(5, 3);
    
        assertThat(point).as("point")
                .extracting(Point2D::getX, Point2D::getY)
                .as("x, y")
                .containsExactly(5.0, 3.0);
    }

For comparison, using extracting(String... propertiesOrFields):

    assertThat(point).as("point")
            .extracting("x", "y")
            .as("x, y")
            .containsExactly(5.0, 3.0);

yields:

java.lang.IllegalArgumentException: The object to extract fields/properties from should not be null
	at org.assertj.core.util.Preconditions.checkArgument(Preconditions.java:131)
	at org.assertj.core.extractor.ByNameMultipleExtractor.apply(ByNameMultipleExtractor.java:36)
	at org.assertj.core.extractor.ByNameMultipleExtractor.apply(ByNameMultipleExtractor.java:24)
	at org.assertj.core.api.AbstractObjectAssert.extracting(AbstractObjectAssert.java:789)

I think the former usage should match the latter behavior.

@stanio
Copy link
Author

stanio commented Feb 23, 2024

I have found the following related issues: #2392 (comment), #2401, #2495. Not sure if the last is meant to address this more specific one.

@stanio
Copy link
Author

stanio commented Feb 23, 2024

I think the former usage should match the latter behavior.

Or more likely, both should match:

        Point2D point = null;

        assertThat(point).as("point").isNotNull()
                .extracting(...
java.lang.AssertionError: [point] 
Expecting actual not to be null

@joel-costigliola
Copy link
Member

I agree we could be more consistent there

@joel-costigliola joel-costigliola added the type: improvement A general improvement label Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A general improvement
Projects
None yet
Development

No branches or pull requests

2 participants