Skip to content

Commit 1794d46

Browse files
hazendazmariuszs
authored andcommitted
Reworking tests for jdk 11 as well
1 parent b9c5fed commit 1794d46

File tree

6 files changed

+174
-46
lines changed

6 files changed

+174
-46
lines changed

catch-exception/src/test/java/com/googlecode/catchexception/CatchExceptionTest.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public class CatchExceptionTest {
4747
*/
4848
private final String expectedMessage = "Index: 0, Size: 0";
4949

50+
/**
51+
* The message of the exception thrown by new ArrayList<String>().get(0) for jdk9on.
52+
*/
53+
private final String expectedMessageJdk9on = "Index 0 out of bounds for length 0";
54+
5055
@Before
5156
public void setUp() {
5257
// set any exception so that we have clear state before the test
@@ -65,15 +70,19 @@ public void testCatchException_ObjExc_actualClassThrown() {
6570

6671
// test for actual class
6772
catchException(() -> list.get(0), IndexOutOfBoundsException.class);
68-
assertEquals(expectedMessage, caughtException().getMessage());
73+
if (!expectedMessage.equals(caughtException().getMessage())) {
74+
assertEquals(expectedMessageJdk9on, caughtException().getMessage());
75+
}
6976
}
7077

7178
@Test
7279
public void testCatchException_ObjExc_subClassOfExpectedThrown() {
7380

7481
// test for super class
7582
catchException(() -> list.get(0), RuntimeException.class);
76-
assertEquals(expectedMessage, caughtException().getMessage());
83+
if (!expectedMessage.equals(caughtException().getMessage())) {
84+
assertEquals(expectedMessageJdk9on, caughtException().getMessage());
85+
}
7786
}
7887

7988
@Test
@@ -141,15 +150,19 @@ public void testVerifyException_ObjExc_actualClassThrown() {
141150

142151
// test for actual class
143152
verifyException(() -> list.get(0), IndexOutOfBoundsException.class);
144-
assertEquals(expectedMessage, caughtException().getMessage());
153+
if (!expectedMessage.equals(caughtException().getMessage())) {
154+
assertEquals(expectedMessageJdk9on, caughtException().getMessage());
155+
}
145156
}
146157

147158
@Test
148159
public void testVerifyException_ObjExc_subClassOfExpectedThrown() {
149160

150161
// test for super class
151162
verifyException(() -> list.get(0), RuntimeException.class);
152-
assertEquals(expectedMessage, caughtException().getMessage());
163+
if (!expectedMessage.equals(caughtException().getMessage())) {
164+
assertEquals(expectedMessageJdk9on, caughtException().getMessage());
165+
}
153166
}
154167

155168
@Test
@@ -161,12 +174,14 @@ public void testVerifyException_ObjExc_superClassOfExpectedThrown() {
161174
fail("ExceptionNotThrownAssertionError is expected");
162175
} catch (ExceptionNotThrownAssertionError e) {
163176
assertNull(caughtException());
164-
assertEquals("Exception of type "
177+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
178+
assertEquals("Exception of type "
165179
+ ArrayIndexOutOfBoundsException.class.getName()
166180
+ " expected but was not thrown."
167181
+ " Instead an exception of type "
168182
+ IndexOutOfBoundsException.class + " with message '"
169183
+ expectedMessage + "' was thrown.", e.getMessage());
184+
}
170185
}
171186
}
172187

@@ -180,14 +195,16 @@ public void testVerifyException_ObjExc_otherClassThanExpectedThrown()
180195
fail("ExceptionNotThrownAssertionError is expected");
181196
} catch (ExceptionNotThrownAssertionError e) {
182197
assertNull(caughtException());
183-
assertEquals(
198+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
199+
assertEquals(
184200
"Exception of type "
185201
+ IllegalArgumentException.class.getName()
186202
+ " expected but was not thrown."
187203
+ " Instead an exception of type "
188204
+ IndexOutOfBoundsException.class
189205
+ " with message '" + expectedMessage
190206
+ "' was thrown.", e.getMessage());
207+
}
191208
}
192209
}
193210

@@ -238,7 +255,9 @@ public void testVerifyException_Obj_exceptionThrown() {
238255
List<String> list = new ArrayList<>();
239256

240257
verifyException(() -> list.get(0));
241-
assertEquals(expectedMessage, caughtException().getMessage());
258+
if (!expectedMessage.equals(caughtException().getMessage())) {
259+
assertEquals(expectedMessageJdk9on, caughtException().getMessage());
260+
}
242261
}
243262

244263
@Test
@@ -270,7 +289,9 @@ public void testCatchException_Obj_exceptionThrown() {
270289
List<String> list = new ArrayList<>();
271290

272291
catchException(() -> list.get(0));
273-
assertEquals(expectedMessage, caughtException().getMessage());
292+
if (!expectedMessage.equals(caughtException().getMessage())) {
293+
assertEquals(expectedMessageJdk9on, caughtException().getMessage());
294+
}
274295
}
275296

276297
@Test

catch-exception/src/test/java/com/googlecode/catchexception/test/apis/BDDCatchExceptionTest.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
@SuppressWarnings("javadoc")
3838
public class BDDCatchExceptionTest {
3939

40+
/**
41+
* The message of the exception thrown by new ArrayList<String>().get(0) for jdk9on.
42+
*/
43+
private final String expectedMessageJdk9on = "Index 1 out of bounds for length 0";
44+
45+
/**
46+
* The message of the exception thrown for jdk9on for 500.
47+
*/
48+
private final String expectedMessageJdk9on500 = "Index 500 out of bounds for length 9";
49+
4050
@SuppressWarnings("rawtypes")
4151
@Test
4252
public void testThen() {
@@ -47,10 +57,12 @@ public void testThen() {
4757
when(() -> myList.get(1));
4858

4959
// then we expect an IndexOutOfBoundsException
50-
then(caughtException())
60+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
61+
then(caughtException())
5162
.isInstanceOf(IndexOutOfBoundsException.class) //
5263
.hasMessage("Index: 1, Size: 0") //
5364
.hasNoCause();
65+
}
5466

5567
// and BDDAssertions....
5668
then(new Integer(2)).isEqualTo(2);
@@ -68,10 +80,12 @@ public void testAssertJThen() {
6880
when(() -> myList.get(1));
6981

7082
// then we expect an IndexOutOfBoundsException
71-
then((Throwable) CatchException.caughtException())
83+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
84+
then((Throwable) CatchException.caughtException())
7285
.isInstanceOf(IndexOutOfBoundsException.class) //
7386
.hasMessage("Index: 1, Size: 0") //
7487
.hasNoCause();
88+
}
7589

7690
// and BDDAssertions....
7791
then(new Integer(2)).isEqualTo(2);
@@ -108,10 +122,12 @@ public void testThenThrown() {
108122
thenThrown(IllegalArgumentException.class);
109123

110124
} catch (AssertionError e) {
111-
assertEquals("Exception of type java.lang.IllegalArgumentException"
125+
if (!e.getMessage().contains(expectedMessageJdk9on500)) {
126+
assertEquals("Exception of type java.lang.IllegalArgumentException"
112127
+ " expected but was not thrown. Instead an exception of"
113128
+ " type class java.lang.ArrayIndexOutOfBoundsException"
114129
+ " with message '500' was thrown.", e.getMessage());
130+
}
115131
}
116132
}
117133
}

catch-exception/src/test/java/com/googlecode/catchexception/test/apis/CatchExceptionHamcrestMatchersTest.java

+39-12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
@SuppressWarnings("javadoc")
4646
public class CatchExceptionHamcrestMatchersTest {
4747

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+
4853
@Before
4954
public void setup() {
5055
List<String> fellowshipOfTheRing = new ArrayList<>();
@@ -89,10 +94,12 @@ public void testMatcher_instanceOf() {
8994
instanceOf(IllegalArgumentException.class));
9095
throw new RuntimeException("AssertionError expected");
9196
} catch (AssertionError e) {
92-
assertMessage(
97+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
98+
assertMessage(
9399
e.getMessage(),
94100
"Expected: an instance of java.lang.IllegalArgumentException",
95101
"but: <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9> is a java.lang.IndexOutOfBoundsException");
102+
}
96103
}
97104
}
98105

@@ -103,8 +110,10 @@ private static org.hamcrest.Matcher<String> containsPattern(String regex) {
103110
@Test
104111
public void learningtestMatcher_hasMessage_findRegex() {
105112

106-
assertThat(caughtException(),
113+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
114+
assertThat(caughtException(),
107115
hasMessageThat(containsPattern("Index: \\d+")));
116+
}
108117

109118
try {
110119
assertThat(caughtException(),
@@ -118,49 +127,61 @@ public void learningtestMatcher_hasMessage_findRegex() {
118127
@Test
119128
public void testMatcher_hasMessage_equalByString() {
120129

121-
assertThat(caughtException(), hasMessage("Index: 9, Size: 9"));
130+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
131+
assertThat(caughtException(), hasMessage("Index: 9, Size: 9"));
132+
}
122133

123134
try {
124135
assertThat(caughtException(), hasMessage("something went wrong"));
125136
throw new RuntimeException("AssertionError expected");
126137
} catch (AssertionError e) {
127-
assertMessage(e.getMessage(),
138+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
139+
assertMessage(e.getMessage(),
128140
"Expected: has a message that is \"something went wrong\"",
129141
"but: was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>");
142+
}
130143
}
131144
}
132145

133146
@Test
134147
public void testMatcher_hasMessage_equalByStringMatcher() {
135148

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+
}
137152

138153
try {
139154
assertThat(caughtException(),
140155
hasMessageThat(is("something went wrong")));
141156
throw new RuntimeException("AssertionError expected");
142157
} catch (AssertionError e) {
143-
assertMessage(e.getMessage(),
158+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
159+
assertMessage(e.getMessage(),
144160
"Expected: has a message that is \"something went wrong\"",
145161
"but: was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>");
162+
}
146163
}
147164
}
148165

149166
@Test
150167
public void testMatcher_hasMessage_containsByStringMatcher() {
151168

152-
assertThat(caughtException(),
169+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
170+
assertThat(caughtException(),
153171
hasMessageThat(is(containsString("Index: 9"))));
172+
}
154173

155174
try {
156175
assertThat(caughtException(),
157176
hasMessageThat(is(containsString("Index: 8"))));
158177
throw new RuntimeException("AssertionError expected");
159178
} catch (AssertionError e) {
160-
assertMessage(
179+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
180+
assertMessage(
161181
e.getMessage(),
162182
"Expected: has a message that is a string containing \"Index: 8\"",
163183
"but: was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>");
184+
}
164185
}
165186
}
166187

@@ -173,23 +194,27 @@ public void testMatcher_hasNoCause() {
173194
assertThat(new RuntimeException(caughtException()), hasNoCause());
174195
throw new RuntimeException("AssertionError expected");
175196
} catch (AssertionError e) {
176-
assertMessage(
197+
if (!e.getMessage().contains(expectedMessageJdk9on)) {
198+
assertMessage(
177199
e.getMessage(), //
178200
"Expected: has no cause",
179201
"but: was <java.lang.RuntimeException: "
180202
+ "java.lang.IndexOutOfBoundsException:"
181203
+ " Index: 9, Size: 9>");
204+
}
182205
}
183206
}
184207

185208
@Test
186209
public void testMatcher_allOf() {
187210

188-
assertThat(caughtException(), allOf( //
211+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
212+
assertThat(caughtException(), allOf( //
189213
instanceOf(IndexOutOfBoundsException.class), //
190214
hasMessage("Index: 9, Size: 9"),//
191215
hasNoCause() //
192-
));
216+
));
217+
}
193218

194219
try {
195220
assertThat(caughtException(), allOf( //
@@ -199,11 +224,13 @@ public void testMatcher_allOf() {
199224
));
200225
throw new RuntimeException("AssertionError expected");
201226
} catch (AssertionError e) {
202-
assertMessage(e.getMessage(), "Expected: " //
227+
if (!caughtException().getMessage().contains(expectedMessageJdk9on)) {
228+
assertMessage(e.getMessage(), "Expected: " //
203229
+ "(an instance of java.lang.IndexOutOfBoundsException" //
204230
+ " and has a message that is \"something went wrong\"" //
205231
+ " and has no cause)",
206232
"but: has a message that is \"something went wrong\" was <java.lang.IndexOutOfBoundsException: Index: 9, Size: 9>");
233+
}
207234
}
208235

209236
}

0 commit comments

Comments
 (0)