source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/ExpectedRootException.java@ 17360

Last change on this file since 17360 was 16618, checked in by simon04, 4 years ago

see #16567 - Fix deprecations related to JUnit 5 (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.testutils;
3
4import static org.openstreetmap.josm.testutils.ThrowableRootCauseMatcher.hasRootCause;
5
6import org.hamcrest.Matcher;
7import org.junit.rules.ExpectedException;
8import org.junit.rules.TestRule;
9import org.junit.runner.Description;
10import org.junit.runners.model.Statement;
11
12import 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
22public 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> &#064;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> &#064;Test
74 * public void throwsExceptionWhoseMessageContainsSpecificText() {
75 * thrown.expectMessage(&quot;happened&quot;);
76 * throw new NullPointerException(&quot;What happened?&quot;);
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> &#064;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> &#064;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}
Note: See TracBrowser for help on using the repository browser.