Skip to content

Commit 4d415f7

Browse files
author
Tomasz Lelek
committed
BAEL-382 null-safety example
1 parent 3351d96 commit 4d415f7

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎kotlin/pom.xml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
<artifactId>kotlin-stdlib</artifactId>
1515
<version>1.0.4</version>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.jetbrains.kotlin</groupId>
19+
<artifactId>kotlin-test-junit</artifactId>
20+
<version>1.0.4</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.12</version>
27+
<scope>test</scope>
28+
</dependency>
1729
</dependencies>
1830

1931
<build>
@@ -24,7 +36,6 @@
2436
<artifactId>kotlin-maven-plugin</artifactId>
2537
<groupId>org.jetbrains.kotlin</groupId>
2638
<version>1.0.4</version>
27-
2839
<executions>
2940
<execution>
3041
<id>compile</id>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung
2+
3+
4+
data class Item(val id: String, val name: String)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung
2+
3+
import java.util.*
4+
5+
class ItemService {
6+
fun findItemNameForId(id: String): Item? {
7+
val itemId = UUID.randomUUID().toString()
8+
return Item(itemId, "name-$itemId");
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung
2+
3+
import org.junit.Test
4+
import kotlin.test.assertNotNull
5+
6+
class ItemServiceTest {
7+
@Test
8+
fun givenItemId_whenGetForOptionalName_shouldMakeActionOnNonNullValue() {
9+
//given
10+
val id = "item_id"
11+
val itemService = ItemService()
12+
13+
//when
14+
val result = itemService.findItemNameForId(id)
15+
16+
//then
17+
assertNotNull(result?.let { it -> it.id })
18+
assertNotNull(result!!.id)
19+
}
20+
}

0 commit comments

Comments
 (0)