* BAEL-756 code for kotlin null-safety article

* BAEL-756 fix typo

* BAEL-756 increment version of Kotlin

* BAEL-756 remove duplicate form pom
This commit is contained in:
Tomasz Lelek 2017-03-29 04:14:32 +02:00 committed by KevinGilmore
parent 361df07694
commit 1a7ccb8248
2 changed files with 143 additions and 4 deletions

View File

@ -90,10 +90,9 @@
<properties> <properties>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<kotlin-maven-plugin.version>1.0.6</kotlin-maven-plugin.version> <kotlin-maven-plugin.version>1.1.1</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.0.6</kotlin-test-junit.version> <kotlin-test-junit.version>1.1.1</kotlin-test-junit.version>
<kotlin-stdlib.version>1.0.6</kotlin-stdlib.version> <kotlin-stdlib.version>1.1.1</kotlin-stdlib.version>
<kotlin-maven-plugin.version>1.0.6</kotlin-maven-plugin.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,140 @@
package com.baeldung.kotlin
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import kotlin.test.assertTrue
class NullSafetyTest {
@Test
fun givenNonNullableField_whenAssignValueToIt_thenNotNeedToCheckAgainstNull() {
//given
var a: String = "value"
//a = null compilation error
//then
assertEquals(a.length, 5)
}
@Test
fun givenNullableField_whenReadValue_thenNeedToCheckAgainstNull() {
//given
var b: String? = "value"
b = null
//when
if (b != null) {
} else {
assertNull(b)
}
}
@Test
fun givenComplexObject_whenUseSafeCall_thenShouldChainCallsResultingWithValue() {
//given
val p: Person? = Person(Country("ENG"))
//when
val res = p?.country?.code
//then
assertEquals(res, "ENG")
}
@Test
fun givenComplexObject_whenUseSafeCall_thenShouldChainCallsResultingWithNull() {
//given
val p: Person? = Person(Country(null))
//when
val res = p?.country?.code
//then
assertNull(res)
}
@Test
fun givenCollectionOfObjects_whenUseLetOperator_thenShouldApplyActionOnlyOnNonNullValue() {
//given
val firstName = "Tom"
val secondName = "Michael"
val names: List<String?> = listOf(firstName, null, secondName)
//when
var res = listOf<String?>()
for (item in names) {
item?.let { res = res.plus(it) }
}
//then
assertEquals(2, res.size)
assertTrue { res.contains(firstName) }
assertTrue { res.contains(secondName) }
}
@Test
fun givenNullableReference_whenUseElvisOperator_thenShouldReturnValueIfReferenceIsNotNull() {
//given
val value: String? = "name"
//when
val res = value?.length ?: -1
//then
assertEquals(res, 4)
}
@Test
fun givenNullableReference_whenUseElvisOperator_thenShouldReturnDefaultValueIfReferenceIsNull() {
//given
val value: String? = null
//when
val res = value?.length ?: -1
//then
assertEquals(res, -1)
}
@Test
fun givenNullableField_whenUsingDoubleExclamationMarkOperatorOnNull_thenThrowNPE() {
//given
var b: String? = "value"
b = null
//when
assertFailsWith<NullPointerException> {
b!!.length
}
}
@Test
fun givenNullableField_whenUsingDoubleExclamationMarkOperatorOnNotNull_thenReturnValue() {
//given
val b: String? = "value"
//then
assertEquals(b!!.length, 5)
}
@Test
fun givenNullableList_whenUseFilterNotNullMethod_thenRemoveALlNullValues() {
//given
val list: List<String?> = listOf("a", null, "b")
//when
val res = list.filterNotNull()
//then
assertEquals(res.size, 2)
assertTrue { res.contains("a") }
assertTrue { res.contains("b") }
}
}
data class Person(val country: Country?)
data class Country(val code: String?)