-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
AssertThrows.java
76 lines (63 loc) · 2.26 KB
/
AssertThrows.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
/*
* Copyright 2015-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.api;
import static java.lang.String.format;
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
import static org.junit.jupiter.api.AssertionUtils.getCanonicalName;
import java.util.function.Supplier;
import org.junit.jupiter.api.function.Executable;
import org.junit.platform.commons.util.UnrecoverableExceptions;
/**
* {@code AssertThrows} is a collection of utility methods that support asserting
* an exception of an expected type is thrown.
*
* @since 5.0
*/
class AssertThrows {
private AssertThrows() {
/* no-op */
}
static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable) {
return assertThrows(expectedType, executable, (Object) null);
}
static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, String message) {
return assertThrows(expectedType, executable, (Object) message);
}
static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable,
Supplier<String> messageSupplier) {
return assertThrows(expectedType, executable, (Object) messageSupplier);
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable,
Object messageOrSupplier) {
try {
executable.execute();
}
catch (Throwable actualException) {
if (expectedType.isInstance(actualException)) {
return (T) actualException;
}
else {
UnrecoverableExceptions.rethrowIfUnrecoverable(actualException);
throw assertionFailure() //
.message(messageOrSupplier) //
.expected(expectedType) //
.actual(actualException.getClass()) //
.reason("Unexpected exception type thrown") //
.cause(actualException) //
.build();
}
}
throw assertionFailure() //
.message(messageOrSupplier) //
.reason(format("Expected %s to be thrown, but nothing was thrown.", getCanonicalName(expectedType))) //
.build();
}
}