1
+ package com.memozr.assertk
2
+
3
+ import com.memozr.assertk.ThrowableAssertion.noCause
4
+ import com.nhaarman.mockito_kotlin.spy
5
+ import com.nhaarman.mockito_kotlin.verify
6
+ import org.assertj.core.api.AbstractThrowableAssert
7
+ import org.assertj.core.api.Assertions
8
+ import org.junit.Test
9
+
10
+ class `Throwable assert test` {
11
+ val aThrowable = Throwable ()
12
+
13
+ private fun aThrowableWithCause () = Throwable (" hello" , aThrowable)
14
+
15
+ lateinit var mockAssertion: AbstractThrowableAssert <* , out Throwable >
16
+ val _expect = object : AssertionHook {
17
+ override fun thatThrownBy (expressionUnderTest : () -> Unit ): ThrowableAssertion {
18
+ mockAssertion = spy(Assertions .assertThatThrownBy(expressionUnderTest))
19
+ return ThrowableAssertion (expressionUnderTest, mockAssertion)
20
+ }
21
+ }
22
+
23
+ @Test
24
+ fun `fails when no exception is thrown` () {
25
+ Assertions .assertThatThrownBy {
26
+ _expect thatThrownBy { }
27
+ }
28
+ }
29
+
30
+ @Test
31
+ fun `passes when exception is thrown` () {
32
+ _expect thatThrownBy { throw Throwable () }
33
+ }
34
+
35
+ @Test
36
+ fun `passes when throwable message matches the specified message` () {
37
+ _expect thatThrownBy { throw Throwable (" hello" ) } hasMessage " hello"
38
+ verify(mockAssertion).hasMessage(" hello" )
39
+ }
40
+
41
+ @Test
42
+ fun `fails when throwable message does not match specified mesage` () {
43
+ expect thatThrownBy {
44
+ _expect thatThrownBy { throw Throwable (" this" ) } hasMessage " that"
45
+ }
46
+ verify(mockAssertion).hasMessage(" that" )
47
+ }
48
+
49
+ @Test
50
+ fun `passes when throwable cause and specified cause match` () {
51
+ _expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable hasMessage " hello"
52
+ _expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable
53
+ verify(mockAssertion).hasCause(aThrowable)
54
+ }
55
+
56
+ @Test
57
+ fun `fails when throwable cause and specified cause do not match` () {
58
+ val exception = Exception ()
59
+ expect thatThrownBy {
60
+ _expect thatThrownBy { throw aThrowableWithCause() } hasCause exception
61
+ }
62
+ verify(mockAssertion).hasCause(exception)
63
+ }
64
+
65
+ @Test
66
+ fun `hasNoCause passes when throwable has no cause` () {
67
+ _expect thatThrownBy { throw aThrowable } has noCause
68
+ verify(mockAssertion).hasNoCause()
69
+ }
70
+
71
+ @Test
72
+ fun `hasNoCause fails when throwable has cause` () {
73
+ expect thatThrownBy {
74
+ _expect thatThrownBy { throw aThrowableWithCause() } has noCause
75
+ }
76
+ verify(mockAssertion).hasNoCause()
77
+ }
78
+
79
+ @Test
80
+ fun `hasMessageStartingWith checks throwable message starts with specified substring` () {
81
+ _expect thatThrownBy { throw Throwable (" exception foo" ) } hasMessageStartingWith " exc"
82
+ verify(mockAssertion).hasMessageStartingWith(" exc" )
83
+ expect thatThrownBy {
84
+ _expect thatThrownBy { throw Throwable (" excepiton foo" ) } hasMessageStartingWith " foo"
85
+ }
86
+ verify(mockAssertion).hasMessageStartingWith(" foo" )
87
+ }
88
+
89
+ @Test
90
+ fun `hasMessageEndingWith checks throwable message ends with specified substring` () {
91
+ _expect thatThrownBy { throw Throwable (" exception foo" ) } hasMessageEndingWith " foo"
92
+ verify(mockAssertion).hasMessageEndingWith(" foo" )
93
+ expect thatThrownBy {
94
+ _expect thatThrownBy { throw Throwable (" exception foo" ) } hasMessageEndingWith " exec"
95
+ }
96
+ verify(mockAssertion).hasMessageEndingWith(" exec" )
97
+ }
98
+
99
+ @Test
100
+ fun `hasMessageContaining checks throwable message ends with specified substring` () {
101
+ _expect thatThrownBy { throw Throwable (" exception foo" ) } hasMessageContaining " foo"
102
+ verify(mockAssertion).hasMessageContaining(" foo" )
103
+
104
+ expect thatThrownBy {
105
+ _expect thatThrownBy { throw Throwable (" exception foo" ) } hasMessageContaining " lololol"
106
+ }
107
+ verify(mockAssertion).hasMessageContaining(" lololol" )
108
+ }
109
+
110
+ @Test
111
+ fun `hasStackTraceContaining checks stacktrace contains specified string` () {
112
+ _expect thatThrownBy { throw aThrowable } hasStackTraceContaining " init"
113
+ verify(mockAssertion).hasStackTraceContaining(" init" )
114
+
115
+ expect thatThrownBy {
116
+ _expect thatThrownBy { throw Throwable (" exception foo" ) } hasStackTraceContaining " not in there!!"
117
+ }
118
+ verify(mockAssertion).hasStackTraceContaining(" not in there!!" )
119
+ }
120
+
121
+ @Test
122
+ fun `hasCauseExactlyInstanceOf checks throwable cause is exactly instance of specified class` () {
123
+ _expect thatThrownBy { throw Throwable (Throwable ()) } hasCauseExactlyInstanceOf Throwable ::class .java
124
+ verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable ::class .java)
125
+
126
+ expect thatThrownBy {
127
+ _expect thatThrownBy { throw Throwable (Exception ()) } hasCauseExactlyInstanceOf Throwable ::class .java
128
+ }
129
+ verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable ::class .java)
130
+
131
+ expect thatThrownBy {
132
+ _expect thatThrownBy { throw Throwable (Throwable ()) } hasCauseExactlyInstanceOf Exception ::class .java
133
+ }
134
+ verify(mockAssertion).hasCauseExactlyInstanceOf(Exception ::class .java)
135
+ }
136
+
137
+ @Test
138
+ fun `block syntax is supported` () {
139
+ _expect thatThrownBy {
140
+ throw Throwable (" exception foo" , Throwable ())
141
+ } and {
142
+ it hasMessage " exception foo"
143
+ it hasCause Throwable ()
144
+ it hasCauseExactlyInstanceOf Throwable ::class .java
145
+ it hasMessageContaining " foo"
146
+ it hasMessageStartingWith " ex"
147
+ it hasMessageEndingWith " foo"
148
+ }
149
+
150
+ _expect thatThrownBy {
151
+ _expect thatThrownBy { throw Throwable (" exception foo" , Throwable ()) } and {
152
+ it hasMessage " blah blah"
153
+ }
154
+ }
155
+ }
156
+ }
0 commit comments