-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
AssertDoesNotThrow.java
91 lines (74 loc) · 2.74 KB
/
AssertDoesNotThrow.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
/*
* Copyright 2015-2022 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 org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
import java.util.function.Supplier;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingSupplier;
import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.commons.util.UnrecoverableExceptions;
import org.opentest4j.AssertionFailedError;
/**
* {@code AssertDoesNotThrow} is a collection of utility methods that support
* explicitly asserting that a given code block does not throw an exception.
*
* @since 5.2
*/
class AssertDoesNotThrow {
private AssertDoesNotThrow() {
/* no-op */
}
static void assertDoesNotThrow(Executable executable) {
assertDoesNotThrow(executable, (Object) null);
}
static void assertDoesNotThrow(Executable executable, String message) {
assertDoesNotThrow(executable, (Object) message);
}
static void assertDoesNotThrow(Executable executable, Supplier<String> messageSupplier) {
assertDoesNotThrow(executable, (Object) messageSupplier);
}
private static void assertDoesNotThrow(Executable executable, Object messageOrSupplier) {
try {
executable.execute();
}
catch (Throwable t) {
UnrecoverableExceptions.rethrowIfUnrecoverable(t);
throw createAssertionFailedError(messageOrSupplier, t);
}
}
static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier) {
return assertDoesNotThrow(supplier, (Object) null);
}
static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, String message) {
return assertDoesNotThrow(supplier, (Object) message);
}
static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Supplier<String> messageSupplier) {
return assertDoesNotThrow(supplier, (Object) messageSupplier);
}
private static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Object messageOrSupplier) {
try {
return supplier.get();
}
catch (Throwable t) {
UnrecoverableExceptions.rethrowIfUnrecoverable(t);
throw createAssertionFailedError(messageOrSupplier, t);
}
}
private static AssertionFailedError createAssertionFailedError(Object messageOrSupplier, Throwable t) {
return assertionFailure() //
.message(messageOrSupplier) //
.reason("Unexpected exception thrown: " + t.getClass().getName() + buildSuffix(t.getMessage())) //
.cause(t) //
.build();
}
private static String buildSuffix(String message) {
return StringUtils.isNotBlank(message) ? ": " + message : "";
}
}