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

Assertions.assertNotNull() should return the non-null object #4015

Open
garydgregory opened this issue Sep 20, 2024 · 8 comments
Open

Assertions.assertNotNull() should return the non-null object #4015

garydgregory opened this issue Sep 20, 2024 · 8 comments

Comments

@garydgregory
Copy link

As a developer, I like to write some test code like:

import static org.junit.jupiter.api.Assertions.assertNotNull;
...
Foo foo = assertNotNull(bar.doSomething()).getFoo();

Deliverables

  • An Assertions.assertNotNull() that returns its input and not void.
  • You could make the same argument for assertSame()
@scordio
Copy link
Contributor

scordio commented Sep 20, 2024

If using AssertJ is an option, with version 3.27.0 it will be possible to write the following:

import static org.assertj.core.api.Assertions.assertThat;
...
Foo foo = assertThat(bar.doSomething()).isNotNull().actual().getFoo();

See assertj/assertj#3489.

@garydgregory
Copy link
Author

Hello @scordio

Thank you for your reply.

I'll be honest with you: I'll never write this kind of extra verbose code. I feel that this obfuscates what the test is trying to show.

The reason I gave this example is because I want less code, not more ;-) I'll stick with the straightforward for now :)

@scordio
Copy link
Contributor

scordio commented Sep 20, 2024

Totally understandable 😉 a pure non-null assertion is not the best showcase for that, but users find it handy for more complex use cases.

As with everything, YMMV 🙂

@garydgregory
Copy link
Author

And do I have miles! 😜

@marcphilipp
Copy link
Member

marcphilipp commented Sep 21, 2024

Changing the result type of assertNotNull would break backward (binary) compatibility. You could use java.util.Objects.requireNonNull instead.

@garydgregory
Copy link
Author

I agree BC is key, this would be for 6.0. Nice idea using requireNonNull 👍

@sbrannen sbrannen changed the title Assertions.assertNotNull() should return the non-null object Assertions.assertNotNull() should return the non-null object Sep 23, 2024
@bjmi
Copy link
Contributor

bjmi commented Oct 5, 2024

The assertNotNull is verbose and superfluous. Just write
Foo foo = bar.doSomething().getFoo();
and Helpful NullPointerExceptions show the null cause.

@garydgregory
Copy link
Author

The assertNotNull is verbose and superfluous. Just write Foo foo = bar.doSomething().getFoo(); and Helpful NullPointerExceptions show the null cause.

Hello @bjmi
JEP 358 is irrelevant here since it requires Java 14 or above. It is a great improvement though! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants