1
+ package com.memozr.assertk
2
+
3
+ import com.memozr.assertk.ThrowableAssertionBuilder.noCause
4
+ import org.assertj.core.api.Assertions
5
+ import org.junit.Test
6
+
7
+ class `ThrownBy test` {
8
+ val aThrowable = Throwable ()
9
+
10
+ private fun aThrowableWithCause () = Throwable (" hello" , aThrowable)
11
+
12
+ @Test
13
+ fun `fails when no exception is thrown` () {
14
+ Assertions .assertThatThrownBy {
15
+ assert thatThrownBy { }
16
+ }
17
+ }
18
+
19
+ @Test
20
+ fun `passes when exception is thrown` () {
21
+ assert thatThrownBy { throw Throwable () }
22
+ }
23
+
24
+ @Test
25
+ fun `passes when throwable message matches the specified message` () {
26
+ assert thatThrownBy { throw Throwable (" hello" ) } hasMessage " hello"
27
+ }
28
+
29
+ @Test
30
+ fun `fails when throwable message does not match specified mesage` () {
31
+ assert thatThrownBy {
32
+ assert thatThrownBy { throw Throwable (" this" ) } hasMessage " that"
33
+ }
34
+ }
35
+
36
+ @Test
37
+ fun `passes when throwable cause and specified cause match` () {
38
+ assert thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable hasMessage " hello"
39
+ assert thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable
40
+ }
41
+
42
+ @Test
43
+ fun `fails when throwable cause and specified cause do not match` () {
44
+ assert thatThrownBy {
45
+ assert thatThrownBy { throw aThrowableWithCause() } hasCause Exception ()
46
+ }
47
+ }
48
+
49
+ @Test
50
+ fun `hasNoCause passes when throwable has no cause` () {
51
+ assert thatThrownBy { throw aThrowable } has noCause
52
+ }
53
+
54
+ @Test
55
+ fun `hasNoCause fails when throwable has cause` () { assert thatThrownBy {
56
+ assert thatThrownBy { throw aThrowableWithCause() } has noCause
57
+ }
58
+ }
59
+
60
+ @Test
61
+ fun `hasMessageStartingWith checks throwable message starts with specified substring` () {
62
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasMessageStartingWith " exc"
63
+ assert thatThrownBy {
64
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasMessageStartingWith " foo"
65
+ }
66
+ }
67
+
68
+ @Test
69
+ fun `hasMessageEndingWith checks throwable message ends with specified substring` () {
70
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasMessageEndingWith " foo"
71
+ assert thatThrownBy {
72
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasMessageEndingWith " exec"
73
+ }
74
+ }
75
+
76
+ @Test
77
+ fun `hasMessageContaining checks throwable message ends with specified substring` () {
78
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasMessageContaining " foo"
79
+ assert thatThrownBy {
80
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasMessageContaining " lololol"
81
+ }
82
+ }
83
+
84
+ @Test
85
+ fun `hasStackTraceContaining checks stacktrace contains specified string` () {
86
+ assert thatThrownBy { throw aThrowable } hasStackTraceContaining " init"
87
+ assert thatThrownBy {
88
+ assert thatThrownBy { throw Throwable (" excepton foo" ) } hasStackTraceContaining " not in there!!"
89
+ }
90
+ }
91
+
92
+ @Test
93
+ fun `hasCauseExactlyInstanceOf checks throwable cause is exactly instance of specified class` () {
94
+ assert thatThrownBy { throw Throwable (Throwable ()) } hasCauseExactlyInstanceOf Throwable ::class .java
95
+
96
+ assert thatThrownBy {
97
+ assert thatThrownBy { throw Throwable (Exception ()) } hasCauseExactlyInstanceOf Throwable ::class .java
98
+ }
99
+
100
+ assert thatThrownBy {
101
+ assert thatThrownBy { throw Throwable (Throwable ()) } hasCauseExactlyInstanceOf Exception ::class .java
102
+ }
103
+ }
104
+
105
+ @Test
106
+ fun `block syntax is supported` () {
107
+ assert thatThrownBy {
108
+ throw Throwable (" exception foo" , Throwable ())
109
+ } and {
110
+ it hasMessage " exception foo"
111
+ it hasCause Throwable ()
112
+ it hasCauseExactlyInstanceOf Throwable ::class .java
113
+ it hasMessageContaining " foo"
114
+ it hasMessageStartingWith " ex"
115
+ it hasMessageEndingWith " foo"
116
+ }
117
+
118
+ assert thatThrownBy {
119
+ assert thatThrownBy { throw Throwable (" exception foo" , Throwable ()) } and {
120
+ it hasMessage " blah blah"
121
+ }
122
+ }
123
+ }
124
+ }
0 commit comments