junit-team / junit5

✅ The 5th major version of the programmer-friendly testing framework for Java and the JVM
https://junit.org
Other
6.33k stars 1.47k forks source link

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

Open garydgregory opened 2 hours ago

garydgregory commented 2 hours ago

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

scordio commented 2 hours ago

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 commented 1 hour ago

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 commented 50 minutes ago

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 🙂