1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.testutils;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.testutils.ThrowableRootCauseMatcher.hasRootCause;
|
---|
5 |
|
---|
6 | import org.hamcrest.Matcher;
|
---|
7 | import org.junit.rules.ExpectedException;
|
---|
8 | import org.junit.rules.TestRule;
|
---|
9 | import org.junit.runner.Description;
|
---|
10 | import org.junit.runners.model.Statement;
|
---|
11 |
|
---|
12 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * The {@code ExpectedRootException} behaves exactly as JUnit's {@link ExpectedException} rule.
|
---|
16 | * This class is needed to add {@link #expectRootCause} method, which has been rejected by JUnit developers,
|
---|
17 | * and {@code ExpectedException} cannot be extended because it has a private constructor.
|
---|
18 | * @see <a href="https://github.com/junit-team/junit4/pull/778">Github pull request</a>
|
---|
19 | * @deprecated Use matchers instead with the return from {@link org.junit.jupiter.api.Assertions#assertThrows}
|
---|
20 | */
|
---|
21 | @Deprecated
|
---|
22 | public final class ExpectedRootException implements TestRule {
|
---|
23 |
|
---|
24 | private final ExpectedException rule = ExpectedException.none();
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Returns a {@linkplain TestRule rule} that expects no exception to be thrown (identical to behavior without this rule).
|
---|
28 | * @return {@code ExpectedRootException} instance
|
---|
29 | */
|
---|
30 | @SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
|
---|
31 | public static ExpectedRootException none() {
|
---|
32 | return new ExpectedRootException();
|
---|
33 | }
|
---|
34 |
|
---|
35 | private ExpectedRootException() {
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Specifies the failure message for tests that are expected to throw an exception but do not throw any.
|
---|
40 | * You can use a {@code %s} placeholder for the description of the expected exception.
|
---|
41 | * E.g. "Test doesn't throw %s." will fail with the error message "Test doesn't throw an instance of foo.".
|
---|
42 | *
|
---|
43 | * @param message exception detail message
|
---|
44 | * @return the rule itself
|
---|
45 | */
|
---|
46 | public ExpectedRootException reportMissingExceptionWithMessage(String message) {
|
---|
47 | rule.reportMissingExceptionWithMessage(message);
|
---|
48 | return this;
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public Statement apply(Statement base, Description description) {
|
---|
53 | return rule.apply(base, description);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Verify that your code throws an exception that is an instance of specific {@code type}.
|
---|
58 | * <pre> @Test
|
---|
59 | * public void throwsExceptionWithSpecificType() {
|
---|
60 | * thrown.expect(NullPointerException.class);
|
---|
61 | * throw new NullPointerException();
|
---|
62 | * }</pre>
|
---|
63 | * @param type Throwable type
|
---|
64 | * @return {@code this}
|
---|
65 | */
|
---|
66 | public ExpectedRootException expect(Class<? extends Throwable> type) {
|
---|
67 | rule.expect(type);
|
---|
68 | return this;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Verify that your code throws an exception whose message contains a specific text.
|
---|
73 | * <pre> @Test
|
---|
74 | * public void throwsExceptionWhoseMessageContainsSpecificText() {
|
---|
75 | * thrown.expectMessage("happened");
|
---|
76 | * throw new NullPointerException("What happened?");
|
---|
77 | * }</pre>
|
---|
78 | * @param substring substring to expect in error message
|
---|
79 | * @return {@code this}
|
---|
80 | */
|
---|
81 | public ExpectedRootException expectMessage(String substring) {
|
---|
82 | rule.expectMessage(substring);
|
---|
83 | return this;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Verify that your code throws an exception whose immediate cause is matched by the given Hamcrest matcher.
|
---|
88 | * <pre> @Test
|
---|
89 | * public void throwsExceptionWhoseCauseCompliesWithMatcher() {
|
---|
90 | * NullPointerException rootCause = new NullPointerException();
|
---|
91 | * IllegalStateException immediateCause = new IllegalStateException(rootCause);
|
---|
92 | * thrown.expectCause(isA(NullPointerException.class));
|
---|
93 | * throw new IllegalArgumentException(immediateCause);
|
---|
94 | * }</pre>
|
---|
95 | * @param expectedCause expected cause
|
---|
96 | * @return {@code this}
|
---|
97 | */
|
---|
98 | public ExpectedRootException expectCause(Matcher<? extends Throwable> expectedCause) {
|
---|
99 | rule.expectCause(expectedCause);
|
---|
100 | return this;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Verify that you code throws an exception whose root cause is matched by the given Hamcrest matcher.
|
---|
105 | * <pre> @Test
|
---|
106 | * public void throwsExceptionWhoseRootCauseCompliesWithMatcher() {
|
---|
107 | * NullPointerException rootCause = new NullPointerException();
|
---|
108 | * IllegalStateException immediateCause = new IllegalStateException(rootCause);
|
---|
109 | * thrown.expectRootCause(isA(NullPointerException.class));
|
---|
110 | * throw new IllegalArgumentException(immediateCause);
|
---|
111 | * }</pre>
|
---|
112 | * @param expectedRootCause expected root cause
|
---|
113 | * @return {@code this}
|
---|
114 | */
|
---|
115 | public ExpectedRootException expectRootCause(Matcher<? extends Throwable> expectedRootCause) {
|
---|
116 | rule.expect(hasRootCause(expectedRootCause));
|
---|
117 | return this;
|
---|
118 | }
|
---|
119 | }
|
---|