Last active
February 25, 2016 16:25
-
-
Save gjesse/878a4ea2acc22a7c2a5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> T?.requireNonNull(id: String): T = this ?: throw NullPointerException("%s must be specified".format(id)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun testRequireNonNull() { | |
val isNull: String? = null | |
val maybeNull: String? = "not really null" | |
assertThatThrownBy { isNull.requireNonNull("isNull") } | |
.isInstanceOf(NullPointerException::class.java) | |
val notNull:String = maybeNull.requireNonNull("maybeNull") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment