1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.googlecode.catchexception.throwable;
17
18 import static com.googlecode.catchexception.throwable.CatchThrowable.catchThrowable;
19 import static com.googlecode.catchexception.throwable.CatchThrowable.caughtThrowable;
20 import static com.googlecode.catchexception.throwable.CatchThrowable.verifyThrowable;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.junit.jupiter.api.Assertions.fail;
25
26 import java.net.HttpRetryException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 class CatchThrowableTest {
37
38
39 private final List<String> list = new ArrayList<>();
40
41
42
43
44 private final String expectedMessage = "Index: 0, Size: 0";
45
46
47
48
49 private final String expectedMessageJdk9on = "Index 0 out of bounds for length 0";
50
51
52
53
54 @BeforeEach
55 void setUp() {
56
57 ThrowableHolder.set(new HttpRetryException("detail", 0));
58 }
59
60
61
62
63 @Test
64 void catchExceptionObjExcNoExceptionThrown() {
65
66 catchThrowable(list::size, IndexOutOfBoundsException.class);
67 assertNull(caughtThrowable());
68 }
69
70
71
72
73 @Test
74 void catchExceptionThrowError() {
75
76 catchThrowable(() -> {
77 throw new Error("ddd");
78 });
79 assertEquals(Error.class, caughtThrowable().getClass());
80 }
81
82
83
84
85 @Test
86 void catchExceptionObjExcActualClassThrown() {
87
88
89 catchThrowable(() -> list.get(0), IndexOutOfBoundsException.class);
90 if (!expectedMessage.equals(caughtThrowable().getMessage())) {
91 assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
92 }
93 }
94
95
96
97
98 @Test
99 void catchExceptionObjExcSubClassOfExpectedThrown() {
100
101
102 catchThrowable(() -> list.get(0), RuntimeException.class);
103 if (!expectedMessage.equals(caughtThrowable().getMessage())) {
104 assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
105 }
106 }
107
108
109
110
111 @Test
112 void catchExceptionObjExcSuperClassOfExpectedThrown() {
113
114 try {
115 catchThrowable(() -> list.get(0), ArrayIndexOutOfBoundsException.class);
116 fail("IndexOutOfBoundsException is expected (shall not be caught)");
117 } catch (IndexOutOfBoundsException e) {
118 assertNull(caughtThrowable());
119 }
120 }
121
122
123
124
125 @Test
126 void catchExceptionObjExcOtherClassThanExpectedThrown() {
127
128 try {
129 catchThrowable(() -> list.get(0), IllegalArgumentException.class);
130 fail("IndexOutOfBoundsException is expected (shall not be caught)");
131 } catch (IndexOutOfBoundsException e) {
132 assertNull(caughtThrowable());
133 }
134 }
135
136
137
138
139 @Test
140 void catchExceptionObjExcMissingArgumentException() {
141
142
143 try {
144 catchThrowable(list::size, null);
145 fail("IllegalArgumentException is expected");
146 } catch (IllegalArgumentException e) {
147 assertEquals("throwableClazz must not be null", e.getMessage());
148 }
149 }
150
151
152
153
154 @Test
155 void catchExceptionObjExcMissingArgumentObject() {
156
157 try {
158 catchThrowable(null, IllegalArgumentException.class);
159 fail("IllegalArgumentException is expected");
160 } catch (IllegalArgumentException e) {
161 assertEquals("obj must not be null", e.getMessage());
162 }
163 }
164
165
166
167
168 @Test
169 void testverifyThrowable_ObjExc_noExceptionThrown() {
170
171 try {
172 verifyThrowable(list::size, IndexOutOfBoundsException.class);
173 fail("ThrowableNotThrownAssertionError is expected");
174 } catch (ThrowableNotThrownAssertionError e) {
175 assertNull(caughtThrowable());
176 assertEquals("Neither a throwable of type " + IndexOutOfBoundsException.class.getName()
177 + " nor another throwable was thrown", e.getMessage());
178 }
179
180 }
181
182
183
184
185 @Test
186 void testverifyThrowable_ObjExc_actualClassThrown() {
187
188
189 verifyThrowable(() -> list.get(0), IndexOutOfBoundsException.class);
190 if (!expectedMessage.equals(caughtThrowable().getMessage())) {
191 assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
192 }
193 }
194
195
196
197
198 @Test
199 void testverifyThrowable_ObjExc_subClassOfExpectedThrown() {
200
201
202 verifyThrowable(() -> list.get(0), RuntimeException.class);
203 if (!expectedMessage.equals(caughtThrowable().getMessage())) {
204 assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
205 }
206 }
207
208
209
210
211 @Test
212 void testverifyThrowable_ObjExc_superClassOfExpectedThrown() {
213
214
215 try {
216 verifyThrowable(() -> list.get(0), ArrayIndexOutOfBoundsException.class);
217 fail("ThrowableNotThrownAssertionError is expected");
218 } catch (ThrowableNotThrownAssertionError e) {
219 assertNull(caughtThrowable());
220 if (!e.getMessage().contains(expectedMessageJdk9on)) {
221 assertEquals("Throwable of type " + ArrayIndexOutOfBoundsException.class.getName()
222 + " expected but was not thrown." + " Instead a throwable of type "
223 + IndexOutOfBoundsException.class + " with message '" + expectedMessage + "' was thrown.",
224 e.getMessage());
225 }
226 }
227 }
228
229
230
231
232 @Test
233 void testverifyThrowable_ObjExc_otherClassThanExpectedThrown() {
234
235
236 try {
237 verifyThrowable(() -> list.get(0), IllegalArgumentException.class);
238 fail("ThrowableNotThrownAssertionError is expected");
239 } catch (ThrowableNotThrownAssertionError e) {
240 assertNull(caughtThrowable());
241 if (!e.getMessage().contains(expectedMessageJdk9on)) {
242 assertEquals("Throwable of type " + IllegalArgumentException.class.getName()
243 + " expected but was not thrown." + " Instead a throwable of type "
244 + IndexOutOfBoundsException.class + " with message '" + expectedMessage + "' was thrown.",
245 e.getMessage());
246 }
247 }
248
249 }
250
251
252
253
254 @Test
255 void testverifyThrowable_ObjExc_missingArgument_Exception() {
256
257
258 try {
259 verifyThrowable(list::size, null);
260 fail("IllegalArgumentException is expected");
261 } catch (IllegalArgumentException e) {
262 assertEquals("throwableClazz must not be null", e.getMessage());
263 }
264 }
265
266
267
268
269 @Test
270 void testverifyThrowable_ObjExc_missingArgument_Object() {
271
272 try {
273 verifyThrowable(null, IllegalArgumentException.class);
274 fail("IllegalArgumentException is expected");
275 } catch (IllegalArgumentException e) {
276 assertEquals("obj must not be null", e.getMessage());
277 }
278 }
279
280
281
282
283 @Test
284 void testverifyThrowable_Obj_noExceptionThrown() {
285
286 List<String> myList = new ArrayList<>();
287
288
289 try {
290 verifyThrowable(myList::size);
291 fail("ThrowableNotThrownAssertionError is expected");
292 } catch (ThrowableNotThrownAssertionError e) {
293 assertNull(caughtThrowable());
294 assertEquals("Throwable expected but not thrown", e.getMessage());
295 }
296 }
297
298
299
300
301 @Test
302 void testverifyThrowable_Obj_exceptionThrown() {
303
304 List<String> myList = new ArrayList<>();
305
306 verifyThrowable(() -> myList.get(0));
307 if (!expectedMessage.equals(caughtThrowable().getMessage())) {
308 assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
309 }
310 }
311
312
313
314
315 @Test
316 void testverifyThrowable_Obj_missingArgument_Object() {
317
318
319 try {
320 verifyThrowable(null);
321 fail("IllegalArgumentException is expected");
322 } catch (IllegalArgumentException e) {
323 assertEquals("obj must not be null", e.getMessage());
324 }
325 }
326
327
328
329
330 @Test
331 void catchExceptionObjNoExceptionThrown() {
332
333 List<String> myList = new ArrayList<>();
334
335
336 catchThrowable(myList::size);
337 assertNull(caughtThrowable());
338 }
339
340
341
342
343 @Test
344 void catchExceptionObjExceptionThrown() {
345
346 List<String> myList = new ArrayList<>();
347
348 catchThrowable(() -> myList.get(0));
349 if (!expectedMessage.equals(caughtThrowable().getMessage())) {
350 assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
351 }
352 }
353
354
355
356
357 @Test
358 void catchExceptionObjMissingArgumentObject() {
359
360
361 try {
362 catchThrowable(null);
363 fail("IllegalArgumentException is expected");
364 } catch (IllegalArgumentException e) {
365 assertEquals("obj must not be null", e.getMessage());
366 }
367 }
368
369
370
371
372 @Test
373 void testProtected() {
374 var obj = new PublicSomethingImpl();
375 catchThrowable(obj::dooo);
376 assertTrue(caughtThrowable() instanceof MyThrowable);
377 }
378
379 }