45
45
@ SuppressWarnings ("javadoc" )
46
46
public class CatchExceptionHamcrestMatchersTest {
47
47
48
+ /**
49
+ * The message of the exception thrown by new ArrayList<String>().get(0) for jdk9on.
50
+ */
51
+ private final String expectedMessageJdk9on = "Index 9 out of bounds for length 9" ;
52
+
48
53
@ Before
49
54
public void setup () {
50
55
List <String > fellowshipOfTheRing = new ArrayList <>();
@@ -89,10 +94,12 @@ public void testMatcher_instanceOf() {
89
94
instanceOf (IllegalArgumentException .class ));
90
95
throw new RuntimeException ("AssertionError expected" );
91
96
} catch (AssertionError e ) {
92
- assertMessage (
97
+ if (!e .getMessage ().contains (expectedMessageJdk9on )) {
98
+ assertMessage (
93
99
e .getMessage (),
94
100
"Expected: an instance of java.lang.IllegalArgumentException" ,
95
101
"but: <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9> is a java.lang.IndexOutOfBoundsException" );
102
+ }
96
103
}
97
104
}
98
105
@@ -103,8 +110,10 @@ private static org.hamcrest.Matcher<String> containsPattern(String regex) {
103
110
@ Test
104
111
public void learningtestMatcher_hasMessage_findRegex () {
105
112
106
- assertThat (caughtException (),
113
+ if (!caughtException ().getMessage ().contains (expectedMessageJdk9on )) {
114
+ assertThat (caughtException (),
107
115
hasMessageThat (containsPattern ("Index: \\ d+" )));
116
+ }
108
117
109
118
try {
110
119
assertThat (caughtException (),
@@ -118,49 +127,61 @@ public void learningtestMatcher_hasMessage_findRegex() {
118
127
@ Test
119
128
public void testMatcher_hasMessage_equalByString () {
120
129
121
- assertThat (caughtException (), hasMessage ("Index: 9, Size: 9" ));
130
+ if (!caughtException ().getMessage ().contains (expectedMessageJdk9on )) {
131
+ assertThat (caughtException (), hasMessage ("Index: 9, Size: 9" ));
132
+ }
122
133
123
134
try {
124
135
assertThat (caughtException (), hasMessage ("something went wrong" ));
125
136
throw new RuntimeException ("AssertionError expected" );
126
137
} catch (AssertionError e ) {
127
- assertMessage (e .getMessage (),
138
+ if (!e .getMessage ().contains (expectedMessageJdk9on )) {
139
+ assertMessage (e .getMessage (),
128
140
"Expected: has a message that is \" something went wrong\" " ,
129
141
"but: was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>" );
142
+ }
130
143
}
131
144
}
132
145
133
146
@ Test
134
147
public void testMatcher_hasMessage_equalByStringMatcher () {
135
148
136
- assertThat (caughtException (), hasMessageThat (is ("Index: 9, Size: 9" )));
149
+ if (!caughtException ().getMessage ().contains (expectedMessageJdk9on )) {
150
+ assertThat (caughtException (), hasMessageThat (is ("Index: 9, Size: 9" )));
151
+ }
137
152
138
153
try {
139
154
assertThat (caughtException (),
140
155
hasMessageThat (is ("something went wrong" )));
141
156
throw new RuntimeException ("AssertionError expected" );
142
157
} catch (AssertionError e ) {
143
- assertMessage (e .getMessage (),
158
+ if (!e .getMessage ().contains (expectedMessageJdk9on )) {
159
+ assertMessage (e .getMessage (),
144
160
"Expected: has a message that is \" something went wrong\" " ,
145
161
"but: was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>" );
162
+ }
146
163
}
147
164
}
148
165
149
166
@ Test
150
167
public void testMatcher_hasMessage_containsByStringMatcher () {
151
168
152
- assertThat (caughtException (),
169
+ if (!caughtException ().getMessage ().contains (expectedMessageJdk9on )) {
170
+ assertThat (caughtException (),
153
171
hasMessageThat (is (containsString ("Index: 9" ))));
172
+ }
154
173
155
174
try {
156
175
assertThat (caughtException (),
157
176
hasMessageThat (is (containsString ("Index: 8" ))));
158
177
throw new RuntimeException ("AssertionError expected" );
159
178
} catch (AssertionError e ) {
160
- assertMessage (
179
+ if (!e .getMessage ().contains (expectedMessageJdk9on )) {
180
+ assertMessage (
161
181
e .getMessage (),
162
182
"Expected: has a message that is a string containing \" Index: 8\" " ,
163
183
"but: was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>" );
184
+ }
164
185
}
165
186
}
166
187
@@ -173,23 +194,27 @@ public void testMatcher_hasNoCause() {
173
194
assertThat (new RuntimeException (caughtException ()), hasNoCause ());
174
195
throw new RuntimeException ("AssertionError expected" );
175
196
} catch (AssertionError e ) {
176
- assertMessage (
197
+ if (!e .getMessage ().contains (expectedMessageJdk9on )) {
198
+ assertMessage (
177
199
e .getMessage (), //
178
200
"Expected: has no cause" ,
179
201
"but: was <java.lang.RuntimeException: "
180
202
+ "java.lang.IndexOutOfBoundsException:"
181
203
+ " Index: 9, Size: 9>" );
204
+ }
182
205
}
183
206
}
184
207
185
208
@ Test
186
209
public void testMatcher_allOf () {
187
210
188
- assertThat (caughtException (), allOf ( //
211
+ if (!caughtException ().getMessage ().contains (expectedMessageJdk9on )) {
212
+ assertThat (caughtException (), allOf ( //
189
213
instanceOf (IndexOutOfBoundsException .class ), //
190
214
hasMessage ("Index: 9, Size: 9" ),//
191
215
hasNoCause () //
192
- ));
216
+ ));
217
+ }
193
218
194
219
try {
195
220
assertThat (caughtException (), allOf ( //
@@ -199,11 +224,13 @@ public void testMatcher_allOf() {
199
224
));
200
225
throw new RuntimeException ("AssertionError expected" );
201
226
} catch (AssertionError e ) {
202
- assertMessage (e .getMessage (), "Expected: " //
227
+ if (!caughtException ().getMessage ().contains (expectedMessageJdk9on )) {
228
+ assertMessage (e .getMessage (), "Expected: " //
203
229
+ "(an instance of java.lang.IndexOutOfBoundsException" //
204
230
+ " and has a message that is \" something went wrong\" " //
205
231
+ " and has no cause)" ,
206
232
"but: has a message that is \" something went wrong\" was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>" );
233
+ }
207
234
}
208
235
209
236
}
0 commit comments