1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.googlecode.catchexception.throwable.apis;
17
18 import static com.googlecode.catchexception.throwable.apis.BDDCatchThrowable.caughtThrowable;
19 import static com.googlecode.catchexception.throwable.apis.BDDCatchThrowable.when;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.assertj.core.api.BDDAssertions;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32 class BDDCatchThrowableTest {
33
34
35
36
37 private final String expectedMessageJdk9on = "Index 1 out of bounds for length 0";
38
39
40
41
42 private final String expectedMessageJdk9on500 = "Index 500 out of bounds for length 9";
43
44
45
46
47 @SuppressWarnings("rawtypes")
48 @Test
49 void then() {
50
51 List myList = new ArrayList();
52
53
54 when(() -> myList.get(1));
55
56
57 if (!caughtThrowable().getMessage().contains(expectedMessageJdk9on)) {
58 BDDAssertions.then(caughtThrowable())
59 .isInstanceOf(IndexOutOfBoundsException.class)
60 .hasMessage("Index: 1, Size: 0")
61 .hasNoCause();
62 }
63
64 }
65
66
67
68
69 @SuppressWarnings("rawtypes")
70 @Test
71 void assertJThen() {
72
73 List myList = new ArrayList();
74
75
76 when(() -> myList.get(1));
77
78
79 if (!caughtThrowable().getMessage().contains(expectedMessageJdk9on)) {
80 BDDAssertions.then(caughtThrowable()).isInstanceOf(IndexOutOfBoundsException.class)
81 .hasMessage("Index: 1, Size: 0")
82 .hasNoCause();
83 }
84
85 }
86
87
88
89
90 @SuppressWarnings("rawtypes")
91 @Test
92 void thenThrown() {
93
94
95 List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
96
97
98 when(() -> myList.get(500));
99
100
101 BDDCatchThrowable.thenThrown(IndexOutOfBoundsException.class);
102
103
104 when(() -> myList.get(0));
105 try {
106 BDDCatchThrowable.thenThrown(IndexOutOfBoundsException.class);
107
108 } catch (AssertionError e) {
109 assertEquals("Neither a throwable of type java.lang." + "IndexOutOfBoundsException nor another throwable "
110 + "was thrown", e.getMessage());
111 }
112
113
114 when(() -> myList.get(500));
115 try {
116 BDDCatchThrowable.thenThrown(IllegalArgumentException.class);
117
118 } catch (AssertionError e) {
119 if (!e.getMessage().contains(expectedMessageJdk9on500)) {
120 assertEquals("Throwable of type java.lang.IllegalArgumentException"
121 + " expected but was not thrown. Instead a throwable of"
122 + " type class java.lang.ArrayIndexOutOfBoundsException" + " with message '500' was "
123 + "thrown.", e.getMessage());
124 }
125 }
126
127 }
128
129 }