1
+ package com.baeldung.conditionalThrowing
2
+
3
+ import org.junit.jupiter.api.Test
4
+ import org.junit.jupiter.api.assertThrows
5
+ import kotlin.test.assertEquals
6
+
7
+
8
+ class MyException (msg : String ) : Exception(msg)
9
+
10
+ inline fun throwIf (throwCondition : Boolean , exProvider : () -> Exception ) {
11
+ if (throwCondition) throw exProvider()
12
+ }
13
+
14
+ inline fun <T : Any ?> T.mustHave (
15
+ otherwiseThrow : (T ) -> Exception = { IllegalStateException ("mustHave: check failed") },
16
+ require : (T ) -> Boolean
17
+ ): T {
18
+ if (! require(this )) throw otherwiseThrow(this )
19
+ return this
20
+ }
21
+
22
+ data class Player (val id : Int , val name : String , val score : Int )
23
+ class InvalidPlayerException (message : String ) : RuntimeException(message)
24
+
25
+ class ConditionalThrowingUnitTest {
26
+
27
+ @Test
28
+ fun `when using require() then get expected results` () {
29
+ val str = " a b c"
30
+ assertThrows<IllegalArgumentException > {
31
+ require(str.length > 10 ) { " The string is too short." }
32
+ }.also { ex -> assertEquals(" The string is too short." , ex.message) }
33
+
34
+ val upperStr = str.also {
35
+ require(it.split(" " ).size == 3 ) { " Format not supported" }
36
+ }.uppercase()
37
+ assertEquals(" A B C" , upperStr)
38
+
39
+
40
+ var nullableValue: String? = null
41
+ assertThrows<IllegalArgumentException > {
42
+ requireNotNull(nullableValue) { " Null is not allowed" }
43
+ }.also { ex -> assertEquals(" Null is not allowed" , ex.message) }
44
+
45
+ nullableValue = " a b c"
46
+ val uppercaseValue = requireNotNull(nullableValue).uppercase()
47
+ assertEquals(" A B C" , uppercaseValue)
48
+ }
49
+
50
+ @Test
51
+ fun `when using check() then get expected results` () {
52
+ val str = " a b c"
53
+ assertThrows<IllegalStateException > {
54
+ check(str.length > 10 ) { " The string is too short." }
55
+ }.also { ex -> assertEquals(" The string is too short." , ex.message) }
56
+
57
+ var nullableValue: String? = null
58
+ assertThrows<IllegalStateException > {
59
+ checkNotNull(nullableValue) { " Null is not allowed" }
60
+ }.also { ex -> assertEquals(" Null is not allowed" , ex.message) }
61
+
62
+ nullableValue = " a b c"
63
+ val uppercaseValue = checkNotNull(nullableValue).uppercase()
64
+ assertEquals(" A B C" , uppercaseValue)
65
+ }
66
+
67
+
68
+ @Test
69
+ fun `when using takeIf() then get expected results` () {
70
+ val str = " a b c"
71
+ assertThrows<MyException > {
72
+ str.takeIf { it.length > 10 } ? : throw MyException (" The string is too short." )
73
+ }.also { ex -> assertEquals(" The string is too short." , ex.message) }
74
+
75
+ val nullIsValid: String? = null
76
+ // we don't expect the exception
77
+ 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) }
80
+ }
81
+
82
+ @Test
83
+ fun `when using throwIf() then get expected results` () {
84
+ val str = " a b c"
85
+ assertThrows<MyException > {
86
+ throwIf(str.length <= 10 ) { MyException (" The string is too short." ) }
87
+ }.also { ex -> assertEquals(" The string is too short." , ex.message) }
88
+
89
+ val uppercaseValue = str.also {
90
+ throwIf(it.split(" " ).size != 3 ) { MyException (" Format not supported" ) }
91
+ }.uppercase()
92
+ assertEquals(" A B C" , uppercaseValue)
93
+ }
94
+
95
+
96
+ @Test
97
+ fun `when using mustHave() then get expected results` () {
98
+ val kai = Player (1 , " Kai" , - 5 )
99
+ assertThrows<IllegalStateException > { kai.mustHave { it.score >= 0 } }
100
+ .also { ex -> assertEquals(" mustHave: check failed" , ex.message) }
101
+
102
+ assertThrows<InvalidPlayerException > {
103
+ kai.mustHave(
104
+ require = { it.score >= 0 },
105
+ otherwiseThrow = { InvalidPlayerException (" Player [id=${it.id} , name=${it.name} ] is invalid" ) }
106
+ )
107
+ }.also { ex -> assertEquals(" Player [id=1, name=Kai] is invalid" , ex.message) }
108
+
109
+ val liam = Player (2 , " Liam" , 42 )
110
+ val upperDescription = liam.mustHave(
111
+ require = { it.score >= 0 },
112
+ otherwiseThrow = { InvalidPlayerException (" Player [id=${it.id} , name=${it.name} ] is invalid" ) }
113
+ ).let { " ${it.name} : ${it.score} " .uppercase() }
114
+ assertEquals(" LIAM : 42" , upperDescription)
115
+ }
116
+ }
0 commit comments