BAEL-382 null-safety example
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user