-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
I think that's a good idea but we'd have to investigate whether the return type can be changed without breaking backward compatibility. |
Tentatively slated for 5.8 M1 solely for the purpose of team discussion. |
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 |
The proposal is analogous to the signature for |
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. |
Please reconsider. |
It's possible with the help of 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 |
Since the original report is also specific to Kotlin I think you're right. 👍 |
Duplicate of #1866 |
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:
Instead, tests like the above code snippet could be simplified to the following:
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.
The text was updated successfully, but these errors were encountered: