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

Introduce assertions for java.util.Optional #1920

Closed
stolle opened this issue Jun 6, 2019 · 7 comments
Closed

Introduce assertions for java.util.Optional #1920

stolle opened this issue Jun 6, 2019 · 7 comments

Comments

@stolle
Copy link

stolle commented Jun 6, 2019

It would be convenient to have some type safe shortcuts to test the content of an Optional for its presence and value. This has been discussed a few times like: https://stackoverflow.com/questions/38954742/assert-that-optional-has-certain-value

The following Assertions methods shall explain, what I have in mind:

    public static void assertEmpty(Optional optional) {
        assertNotNull(optional);
        assertTrue(optional.isEmpty());
    }

    public static void assertPresent(Optional optional) {
        assertNotNull(optional);
        assertTrue(optional.isPresent());
    }

    public static <T> void assertContains(T expected, Optional<T> actual) {
        assertNotNull(expected);
        assertPresent(actual);
        // maybe even more strict: assertEquals(optional.get().getClass(), value.getClass());
        assertEquals(actual.get(), expected);
    }
@sormuras
Copy link
Member

sormuras commented Jun 6, 2019

Interesting idea.

assertPresent() could also return the object contained in the optional instance, similar to assertThrows().

@stolle
Copy link
Author

stolle commented Jun 6, 2019

Yep, definitely. This would look like:

public static <T> T assertPresent(final Optional<T> optional) {
    assertNotNull(optional);
    assertTrue(optional.isPresent());
    return optional.get();
}

@stolle
Copy link
Author

stolle commented Jun 6, 2019

And there should be methods for OptionalLong, OptionalInt and OptionalDouble as well.

@sbrannen
Copy link
Member

sbrannen commented Jun 6, 2019

To be perfectly honest, I'm not convinced that we should be adding additional categories of assertions to JUnit Jupiter.

Third-party frameworks like AssertJ already provide excellent support for java.util.Optional.

@junit-team/junit-lambda, thoughts?

@sbrannen sbrannen changed the title Assertions for java.util.Optional Introduce assertions for java.util.Optional Jun 6, 2019
@marcphilipp
Copy link
Member

I agree.

@sormuras
Copy link
Member

sormuras commented Jun 7, 2019

Convinced.

@sbrannen
Copy link
Member

sbrannen commented Jun 7, 2019

Team Decision: we will not introduce dedicated assertions for Optional, and we encourage developers to use third-party assertion frameworks when greater power and/or expressiveness is desired.

Quoting from the User Guide:

Even though the assertion facilities provided by JUnit Jupiter are sufficient for many testing scenarios, there are times when more power and additional functionality such as matchers are desired or required. In such cases, the JUnit team recommends the use of third-party assertion libraries such as AssertJ, Hamcrest, Truth, etc. Developers are therefore free to use the assertion library of their choice.

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