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

NullPointerException when expecting null in .returns and .doesNotReturn #2901

Closed
ines-exner opened this issue Jan 6, 2023 · 5 comments
Closed
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@ines-exner
Copy link

ines-exner commented Jan 6, 2023

Describe the bug
When I try to use AbstractObjectAssert::returns and I want to check that the function I pass returns null for something, the test throws a NullPointerException:
java.lang.NullPointerException
at org.assertj.core.api.AbstractObjectAssert.returns(AbstractObjectAssert.java:1105)

The same thing also happens for doesNotReturn.

  • assertj core version: 3.24.0
  • java version: 11
  • test framework version: jUnit 5
  • os (if relevant): no

Test case reproducing the bug

Add a test case showing the bug that we can run

import org.junit.jupiter.api.Test;

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

class TestReturns {
    class TestObject {
        final String a;
        TestObject(final String a) {
            this.a = a;
        }

        String getA() {
            return a;
        }
    }

    @Test
    void getA_should_return_null() {
        final TestObject objectUnderTest = new TestObject(null);
        assertThat(objectUnderTest).returns(null, TestObject::getA);
    }

    @Test
    void getA_should_not_return_null() {
        final TestObject objectUnderTest = new TestObject("astring");
        assertThat(objectUnderTest).doesNotReturn(null, TestObject::getA);
    }
}
@scordio
Copy link
Member

scordio commented Jan 6, 2023

Thanks for the bug report, @ines-exner. This is a regression caused by #2725. I'll look into it.

@scordio scordio added type: bug A general bug type: regression A regression from a previous release and removed type: bug A general bug labels Jan 6, 2023
@scordio scordio self-assigned this Jan 6, 2023
@ines-exner
Copy link
Author

I've also looked into this and it's this line that throws: Objects objects = getComparatorBasedObjectAssertions(expected.getClass()); (expected is null in our usecase)
This could be fixed by doing something like this instead:

  public <T> SELF doesNotReturn(T expected, Function<ACTUAL, T> from) {
    requireNonNull(from, "The given getter method/Function must not be null");
    Objects objects = getComparatorBasedObjectAssertions(expected);
    objects.assertNotEqual(info, from.apply(actual), expected);
    return myself;
  }

  private <T> Objects getComparatorBasedObjectAssertions(T valueOfType) {
    if (valueOfType == null) {
      return objects;
    }
    final Class<?> type = valueOfType.getClass();
    TypeComparators comparatorsByType = getComparatorsByType();
    if (comparatorsByType.hasComparatorForType(type)) {
      return new Objects(new ComparatorBasedComparisonStrategy(comparatorsByType.getComparatorForType(type)));
    }
    return objects;
  }

@scordio scordio closed this as completed in bb2d22f Jan 6, 2023
@scordio scordio added this to the 3.24.1 milestone Jan 6, 2023
@scordio
Copy link
Member

scordio commented Jan 6, 2023

Thanks again and sorry for the oversight, we should have had such a simple test case 😓

I'm going to release 3.24.1 soon.

@scordio
Copy link
Member

scordio commented Jan 6, 2023

@ines-exner 3.24.1 is now available.

@ines-exner
Copy link
Author

Worked perfectly, thanks a lot for the quick response 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

2 participants