Skip to content

Commit a4bced1

Browse files
committed
[conditional-exception] update
1 parent 113574b commit a4bced1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

core-kotlin-modules/core-kotlin-exceptions/src/test/kotlin/com/baeldung/conditionalThrowing/ConditionalThrowingUnitTest.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ inline fun throwIf(throwCondition: Boolean, exProvider: () -> Exception) {
1212
}
1313

1414
inline fun <T : Any?> T.mustHave(
15-
otherwiseThrow: (T) -> Exception = { IllegalStateException("mustHave: check failed") },
15+
otherwiseThrow: (T) -> Exception = { IllegalStateException("mustHave check failed: $it") },
1616
require: (T) -> Boolean
1717
): T {
1818
if (!require(this)) throw otherwiseThrow(this)
@@ -29,7 +29,8 @@ class ConditionalThrowingUnitTest {
2929
val str = "a b c"
3030
assertThrows<IllegalArgumentException> {
3131
require(str.length > 10) { "The string is too short." }
32-
}.also { ex -> assertEquals("The string is too short.", ex.message) }
32+
null.toString()
33+
}
3334

3435
val upperStr = str.also {
3536
require(it.split(" ").size == 3) { "Format not supported" }
@@ -40,7 +41,7 @@ class ConditionalThrowingUnitTest {
4041
var nullableValue: String? = null
4142
assertThrows<IllegalArgumentException> {
4243
requireNotNull(nullableValue) { "Null is not allowed" }
43-
}.also { ex -> assertEquals("Null is not allowed", ex.message) }
44+
}
4445

4546
nullableValue = "a b c"
4647
val uppercaseValue = requireNotNull(nullableValue).uppercase()
@@ -52,12 +53,12 @@ class ConditionalThrowingUnitTest {
5253
val str = "a b c"
5354
assertThrows<IllegalStateException> {
5455
check(str.length > 10) { "The string is too short." }
55-
}.also { ex -> assertEquals("The string is too short.", ex.message) }
56+
}
5657

5758
var nullableValue: String? = null
5859
assertThrows<IllegalStateException> {
5960
checkNotNull(nullableValue) { "Null is not allowed" }
60-
}.also { ex -> assertEquals("Null is not allowed", ex.message) }
61+
}
6162

6263
nullableValue = "a b c"
6364
val uppercaseValue = checkNotNull(nullableValue).uppercase()
@@ -70,21 +71,21 @@ class ConditionalThrowingUnitTest {
7071
val str = "a b c"
7172
assertThrows<MyException> {
7273
str.takeIf { it.length > 10 } ?: throw MyException("The string is too short.")
73-
}.also { ex -> assertEquals("The string is too short.", ex.message) }
74+
}
7475

7576
val nullIsValid: String? = null
7677
// we don't expect the exception
7778
assertThrows<MyException> {
78-
nullIsValid.takeIf { it == null || it.length > 10 } ?: throw MyException("The string is too short.")
79-
}.also { ex -> assertEquals("The string is too short.", ex.message) }
79+
nullIsValid.takeIf { true } ?: throw MyException("The string is too short.")
80+
}
8081
}
8182

8283
@Test
8384
fun `when using throwIf() then get expected results`() {
8485
val str = "a b c"
8586
assertThrows<MyException> {
8687
throwIf(str.length <= 10) { MyException("The string is too short.") }
87-
}.also { ex -> assertEquals("The string is too short.", ex.message) }
88+
}
8889

8990
val uppercaseValue = str.also {
9091
throwIf(it.split(" ").size != 3) { MyException("Format not supported") }
@@ -97,7 +98,7 @@ class ConditionalThrowingUnitTest {
9798
fun `when using mustHave() then get expected results`() {
9899
val kai = Player(1, "Kai", -5)
99100
assertThrows<IllegalStateException> { kai.mustHave { it.score >= 0 } }
100-
.also { ex -> assertEquals("mustHave: check failed", ex.message) }
101+
.also { ex -> assertEquals("mustHave check failed: Player(id=1, name=Kai, score=-5)", ex.message) }
101102

102103
assertThrows<InvalidPlayerException> {
103104
kai.mustHave(

0 commit comments

Comments
 (0)