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

Add non-null return type to assertNotNull method #2477

Closed
adil-hussain-84 opened this issue Nov 20, 2020 · 11 comments
Closed

Add non-null return type to assertNotNull method #2477

adil-hussain-84 opened this issue Nov 20, 2020 · 11 comments

Comments

@adil-hussain-84
Copy link

It would be sweet if the assertNotNull(actual:) method returned the object that is passed into it back out as a non-null type.

This would help with writing JUnit 5 tests in Kotlin big time and avoid the need to force unwrap properties like this:

assertNotNull(myObject?.myProperty)
val myProperty = myObject!!.myProperty
assertEquals("Foo", myProperty.someOtherProperty)

Instead, tests like the above code snippet could be simplified to the following:

val myProperty = assertNotNull(myObject?.myProperty)
assertEquals("Foo", myProperty.someOtherProperty)

Note that the kotlin-test library already offers this mechanism in its assertNotNull method, and Apple offers a similar XCTUnwrap method in their XCTest framework.

@marcphilipp
Copy link
Member

I think that's a good idea but we'd have to investigate whether the return type can be changed without breaking backward compatibility.

@marcphilipp
Copy link
Member

Tentatively slated for 5.8 M1 solely for the purpose of team discussion.

@sormuras
Copy link
Member

If such a refactoring breaks the existing API, we could introduce new methods that a) delegate to the existing ones for the null-check and b) return the given values. Similar to Objects.requireNonNull from the java.util package.

@sbrannen
Copy link
Member

The proposal is analogous to the signature for org.junit.platform.commons.util.Preconditions.notNull(T, String).

@marcphilipp marcphilipp modified the milestones: 5.8 M1, 5.8 M2/RC1 Feb 7, 2021
@marcphilipp marcphilipp removed this from the 5.8 RC1 milestone Aug 15, 2021
@stale
Copy link

stale bot commented Aug 15, 2022

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the status: stale label Aug 15, 2022
@ephemient
Copy link

Please reconsider.

@awelless
Copy link

awelless commented Mar 30, 2023

It's possible with the help of kotlin.contracts.ContractBuilder.
I can prepare a pull request containing assertNonNull and assertInstanceOf methods.

In the meantime, you can write these methods in your repo by yourself:

@OptIn(ExperimentalContracts::class)
fun assertNotNull(actual: Any?) {
    contract {
        returns() implies (actual != null)
    }

    Assertions.assertNotNull(actual)
}

@OptIn(ExperimentalContracts::class)
inline fun <reified T> assertInstanceOf(actual: Any): T {
    contract {
        returns() implies (actual is T)
    }

    return assertInstanceOf(T::class.java, actual)
}

@marcphilipp
Copy link
Member

@awelless see #1866

@awelless
Copy link

@marcphilipp
I noticed this issue first. I think, it can be closed as a duplicate of #1866

@marcphilipp
Copy link
Member

Since the original report is also specific to Kotlin I think you're right. 👍

@marcphilipp
Copy link
Member

Duplicate of #1866

@marcphilipp marcphilipp marked this as a duplicate of #1866 Apr 29, 2023
@marcphilipp marcphilipp closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2023
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

7 participants