-
Notifications
You must be signed in to change notification settings - Fork 183
/
ExceptionAssertionsExamples.java
376 lines (334 loc) · 18.7 KB
/
ExceptionAssertionsExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.assertj.core.api.BDDAssertions.thenCode;
import static org.assertj.core.api.BDDAssertions.thenNoException;
import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Exception assertions examples.
*
* @author Joel Costigliola
*/
public class ExceptionAssertionsExamples extends AbstractAssertionsExamples {
@Test
public void exceptions_assertions_examples() {
Exception cause = new Exception("chemical explosion");
Exception e = new RuntimeException("Boom!", cause);
// you can check exception type
assertThat(e).isInstanceOf(RuntimeException.class);
// you can check exception's cause
assertThat(e).hasCause(cause)
.hasCauseReference(cause)
.hasCauseInstanceOf(Exception.class);
// you can check exception message (with String#format syntax support)
assertThat(e).hasMessage("Boom!")
.hasMessage("%s!", "Boom")
.hasRootCauseMessage("chemical explosion")
.hasRootCauseMessage("chemical %s", "explosion");
// sometimes message are not entirely predictible, you can then check for start, end or containing string.
assertThat(e).hasMessageStartingWith("Boo")
.hasMessageContaining("oom")
.hasMessageContainingAll("Boo", "oom")
.hasMessageNotContainingAny("bar", "foo")
.hasMessageEndingWith("!");
// this equivalent to (unless for error message which is more explicit in assertThat(e).hasMessageXXX)
assertThat(e.getMessage()).startsWith("Boo")
.contains("oom")
.endsWith("!");
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
assertThat(throwableWithMessage).hasMessage("wrong amount 123")
.hasMessage("%s amount %d", "wrong", 123)
.hasMessageStartingWith("wrong")
.hasMessageStartingWith("%s a", "wrong")
.hasMessageContaining("wrong amount")
.hasMessageContaining("wrong %s", "amount")
.hasMessageContainingAll("wrong", "amount")
.hasMessageEndingWith("123")
.hasMessageEndingWith("amount %s", "123")
.hasMessageMatching("wrong amount .*")
.hasMessageNotContaining("right")
.hasMessageNotContainingAny("right", "price")
.hasStackTraceContaining("%s amount", "wrong");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
throw new RuntimeException(new IllegalArgumentException("cause message"));
})
.havingCause()
.withMessage("cause message");
}
@Test
public void stack_trace_filtering() {
// stack trace filtering is handy for Eclipse integration, because when a test fails Eclipse JUnit view
// won't be cleaned of AssertJ statck trace elements.
// check the err output to see the difference between a classic and a filtered stack trace.
System.err.println("--------------- stack trace not filtered -----------------");
Assertions.setRemoveAssertJRelatedElementsFromStackTrace(false);
try {
assertThat("Messi").isEqualTo("Ronaldo");
} catch (AssertionError e) {
assertThat(e).hasStackTraceContaining("newAssertionError");
e.printStackTrace();
}
// --------------- stack trace not filtered -----------------
// org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'>
// at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
// at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
// at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
// at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
// at org.assertj.core.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:34)
// at org.assertj.core.error.ShouldBeEqual.newComparisonFailure(ShouldBeEqual.java:180)
// at org.assertj.core.error.ShouldBeEqual.comparisonFailure(ShouldBeEqual.java:171)
// at org.assertj.core.error.ShouldBeEqual.newAssertionError(ShouldBeEqual.java:119)
// at org.assertj.core.internal.Failures.failure(Failures.java:73)
// at org.assertj.core.internal.Objects.assertEqual(Objects.java:138)
// at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:86)
// at
// org.assertj.core.examples.ExceptionAssertionsExamples.stack_trace_filtering(ExceptionAssertionsExamples.java:56)
// at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
// at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
// at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
// at java.lang.reflect.Method.invoke(Method.java:616)
// at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
// at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
// at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
// at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
// at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
// at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
// at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
// at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
// at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
// at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
// at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
// at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
// at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
// at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
// at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
// at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
System.err.println("\n--------------- stack trace filtered -----------------");
Assertions.setRemoveAssertJRelatedElementsFromStackTrace(true);
try {
assertThat("Messi").isEqualTo("Ronaldo");
} catch (AssertionError e) {
e.printStackTrace();
e.getStackTrace();
}
}
// see below that elements :
// at org.assertj.core.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:34)
// at org.assertj.core.error.ShouldBeEqual.newComparisonFailure(ShouldBeEqual.java:180)
// at org.assertj.core.error.ShouldBeEqual.comparisonFailure(ShouldBeEqual.java:171)
// at org.assertj.core.error.ShouldBeEqual.newAssertionError(ShouldBeEqual.java:119)
// at org.assertj.core.internal.Failures.failure(Failures.java:73)
// at org.assertj.core.internal.Objects.assertEqual(Objects.java:138)
// at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:86)
// at
// org.assertj.core.examples.ExceptionAssertionsExamples.stack_trace_filtering(ExceptionAssertionsExamples.java:56)
// don't appear in :
// --------------- stack trace filtered -----------------
// org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'>
// at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
// at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
// at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
// at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
// at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
// at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
// at java.lang.reflect.Method.invoke(Method.java:616)
// at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
// at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
// at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
// at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
// at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
// at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
// at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
// at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
// at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
// at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
// at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
// at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
// at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
// at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
// at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
// at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
@Test
public void exception_cause_assertion_examples() {
NullPointerException cause = new NullPointerException("boom!");
Throwable throwable = new Throwable(cause);
assertThat(throwable).hasCause(cause)
// hasCauseInstanceOf will match inheritance.
.hasCauseInstanceOf(NullPointerException.class)
.hasCauseInstanceOf(RuntimeException.class)
// hasCauseExactlyInstanceOf will match only exact same type
.hasCauseExactlyInstanceOf(NullPointerException.class);
// navigate before checking
assertThat(throwable).getCause()
.hasMessage("boom!")
.hasMessage("%s!", "boom")
.hasMessageStartingWith("bo")
.hasMessageEndingWith("!")
.hasMessageContaining("boo")
.hasMessageContainingAll("bo", "oom", "!")
.hasMessageMatching("b...!")
.hasMessageNotContaining("bam")
.hasMessageNotContainingAny("bam", "bim")
// isInstanceOf will match inheritance.
.isInstanceOf(NullPointerException.class)
.isInstanceOf(RuntimeException.class)
// isExactlyInstanceOf will match only exact same type
.isExactlyInstanceOf(NullPointerException.class);
}
@Test
public void exception_root_cause_assertion_examples() {
NullPointerException rootCause = new NullPointerException("null!");
Throwable throwable = new Throwable(new IllegalStateException(rootCause));
assertThat(throwable).hasRootCause(rootCause)
.hasRootCauseMessage("null!")
.hasRootCauseMessage("%s!", "null")
// hasRootCauseInstanceOf will match inheritance
.hasRootCauseInstanceOf(NullPointerException.class)
.hasRootCauseInstanceOf(RuntimeException.class)
// hasRootCauseExactlyInstanceOf will match only exact same type
.hasRootCauseExactlyInstanceOf(NullPointerException.class);
// navigate before checking
assertThat(throwable).getRootCause()
.hasMessage("null!")
.hasMessage("%s!", "null")
.hasMessageStartingWith("nu")
.hasMessageEndingWith("!")
.hasMessageContaining("ul")
.hasMessageContainingAll("nu", "ull", "l!")
.hasMessageMatching("n...!")
.hasMessageNotContaining("NULL")
.hasMessageNotContainingAny("Null", "NULL")
// isInstanceOf will match inheritance.
.isInstanceOf(NullPointerException.class)
.isInstanceOf(RuntimeException.class)
// isExactlyInstanceOf will match only exact same type
.isExactlyInstanceOf(NullPointerException.class);
}
@Test
public void thrown_exception_assertion_examples() {
// @format:off
assertThatThrownBy(() -> { throw new IllegalArgumentException("boom!"); })
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("boom");
// @format:on
}
@Test
public void thrown_exception_assertion_alternative() {
// @format:off
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> { throw new IOException("boom!"); })
.withMessage("boom!")
.withMessageContaining("oom")
.withMessage("%s!", "boom")
.withStackTraceContaining("IOException")
.withNoCause();
// @format:on
}
@Test
public void thrown_exception_assertion_alternative_withStackTraceContaining() {
// @format:off
Throwable runtime = new RuntimeException("no way", new Exception("you shall not pass"));
assertThat(runtime).hasCauseInstanceOf(Exception.class)
.hasStackTraceContaining("no way")
.hasStackTraceContaining("you shall not pass");
// assertion will pass
assertThatExceptionOfType(RuntimeException.class)
.as("check passage")
.isThrownBy(() -> {throw runtime;})
.withStackTraceContaining("you shall not pass");
assertThatNullPointerException()
.as("check explosion")
.isThrownBy(() -> {throw new NullPointerException("null !");})
.withMessage("null !");
assertThatIllegalArgumentException()
.isThrownBy(() -> {throw new IllegalArgumentException("that's illegal !");})
.withMessage("that's illegal !");
assertThatIOException().isThrownBy(() -> { throw new IOException("boom!"); })
.withMessage("boom!")
.withMessageContaining("oom")
.withMessage("%s!", "boom")
.withStackTraceContaining("IOException")
.withNoCause();
// @format:on
}
@Test
public void bdd_style_exception_testing() {
// given
String[] names = { "Pier ", "Pol", "Jak" };
// when
Throwable thrown = catchThrowable(() -> System.out.println(names[10]));
// then
assertThat(thrown).isInstanceOf(ArrayIndexOutOfBoundsException.class)
.hasMessageContaining("10");
}
@Test
public void use_as_with_exception_testing() {
// @format:off
Throwable assertionError = catchThrowable(() -> {
assertThat(catchThrowable(() -> { throw new Exception("boom!"); }))
.as("boom expected")
.isInstanceOf(Exception.class)
// make the assertion fail
.hasMessageContaining("bam");
});
// @format:on
assertThat(assertionError).isInstanceOf(AssertionError.class)
.hasMessageContaining("[boom expected]");
// same stuff with assertThatCode
// @format:off
assertionError = catchThrowable(() -> {
assertThatCode(() -> { throw new Exception("boom!");})
.as("boom expected")
.isInstanceOf(Exception.class)
// make the assertion fail
.hasMessageContaining("bam");
});
// @format:on
assertThat(assertionError).isInstanceOf(AssertionError.class)
.hasMessageContaining("[boom expected]");
}
@Test
public void check_code_does_not_throw_exceptions() {
assertThatNoException().isThrownBy(() -> System.out.println("OK"));
// or
thenNoException().isThrownBy(() -> System.out.println("OK"));
// or
assertThatCode(() -> {}).doesNotThrowAnyException();
// or
thenCode(() -> {}).doesNotThrowAnyException();
}
@Test
public void catchThrowableOfType_examples() {
assertThat(catchThrowableOfType(() -> {}, Exception.class)).isNull();
}
}