BAEL-382 null-safety example

This commit is contained in:
Tomasz Lelek
2017-01-02 14:48:05 +01:00
parent 3351d96147
commit 4d415f7f74
4 changed files with 46 additions and 1 deletions
@@ -0,0 +1,4 @@
package com.baeldung
data class Item(val id: String, val name: String)
@@ -0,0 +1,10 @@
package com.baeldung
import java.util.*
class ItemService {
fun findItemNameForId(id: String): Item? {
val itemId = UUID.randomUUID().toString()
return Item(itemId, "name-$itemId");
}
}
@@ -0,0 +1,20 @@
package com.baeldung
import org.junit.Test
import kotlin.test.assertNotNull
class ItemServiceTest {
@Test
fun givenItemId_whenGetForOptionalName_shouldMakeActionOnNonNullValue() {
//given
val id = "item_id"
val itemService = ItemService()
//when
val result = itemService.findItemNameForId(id)
//then
assertNotNull(result?.let { it -> it.id })
assertNotNull(result!!.id)
}
}