BAEL-2650 - Change scope functions tests to use assertTrue (#6558)

* BAEL-2650 - Kotlin standard functions: run, with, let, also and apply

* BAEL-2650 - Change scope functions tests to use assertTrue
This commit is contained in:
juanvaccari 2019-03-18 23:59:04 +00:00 committed by maibin
parent 8005f6b313
commit 92b03457a1
1 changed files with 35 additions and 11 deletions

View File

@ -1,7 +1,8 @@
package com.baeldung.scope package com.baeldung.scope
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test import org.junit.Test
import kotlin.test.assertTrue
class ScopeFunctionsUnitTest { class ScopeFunctionsUnitTest {
@ -24,7 +25,10 @@ class ScopeFunctionsUnitTest {
it.append("It takes a StringBuilder instance and returns the number of characters in the generated String") it.append("It takes a StringBuilder instance and returns the number of characters in the generated String")
it.length it.length
} }
assertThat(numberOfCharacters).isEqualTo(128)
assertTrue {
numberOfCharacters == 128
}
} }
@Test @Test
@ -35,14 +39,18 @@ class ScopeFunctionsUnitTest {
"At this point is safe to reference the variable. Let's print the message: $it" "At this point is safe to reference the variable. Let's print the message: $it"
} ?: "default value" } ?: "default value"
assertThat(charactersInMessage).isEqualTo("At this point is safe to reference the variable. Let's print the message: hello there!") assertTrue {
charactersInMessage.equals("At this point is safe to reference the variable. Let's print the message: hello there!")
}
val aNullMessage = null val aNullMessage = null
val thisIsNull = aNullMessage?.let { val thisIsNull = aNullMessage?.let {
"At this point it would be safe to reference the variable. But it will not really happen because it is null. Let's reference: $it" "At this point it would be safe to reference the variable. But it will not really happen because it is null. Let's reference: $it"
} ?: "default value" } ?: "default value"
assertThat(thisIsNull).isEqualTo("default value") assertTrue {
thisIsNull.equals("default value")
}
} }
@Test @Test
@ -52,7 +60,10 @@ class ScopeFunctionsUnitTest {
name = "Mary" name = "Mary"
surname = "Smith" surname = "Smith"
} }
assertThat(aStudent.name).isEqualTo("Mary")
assertTrue {
aStudent.name.equals("Mary")
}
} }
@Test @Test
@ -62,7 +73,9 @@ class ScopeFunctionsUnitTest {
.setName("Martha") .setName("Martha")
.setSurname("Spector") .setSurname("Spector")
assertThat(teacher.surname).isEqualTo("Spector") assertTrue {
teacher.surname.equals("Spector")
}
} }
@Test @Test
@ -76,14 +89,19 @@ class ScopeFunctionsUnitTest {
.also { logger.info(it.toString()) } .also { logger.info(it.toString()) }
.headers .headers
assertThat(logger.wasCalled()).isTrue() assertTrue {
assertThat(headers.headerInfo).isEqualTo("some header info") logger.wasCalled() && headers.headerInfo.equals("some header info")
}
} }
@Test @Test
fun shouldInitializeFieldWhenAlsoUsed() { fun shouldInitializeFieldWhenAlsoUsed() {
val aStudent = Student().also { it.name = "John"} val aStudent = Student().also { it.name = "John"}
assertThat(aStudent.name).isEqualTo("John")
assertTrue {
aStudent.name.equals("John")
}
} }
@Test @Test
@ -104,7 +122,10 @@ class ScopeFunctionsUnitTest {
append("It takes a StringBuilder instance and returns the number of characters in the generated String") append("It takes a StringBuilder instance and returns the number of characters in the generated String")
length length
} }
assertThat(numberOfCharacters).isEqualTo(128)
assertTrue {
numberOfCharacters == 128
}
} }
@Test @Test
@ -113,7 +134,10 @@ class ScopeFunctionsUnitTest {
val charactersInMessage = message?.run { val charactersInMessage = message?.run {
"At this point is safe to reference the variable. Let's print the message: $this" "At this point is safe to reference the variable. Let's print the message: $this"
} ?: "default value" } ?: "default value"
assertThat(charactersInMessage).isEqualTo("At this point is safe to reference the variable. Let's print the message: hello there!")
assertTrue {
charactersInMessage.equals("At this point is safe to reference the variable. Let's print the message: hello there!")
}
} }
} }