From 8e80a2a7b89329f95cb41e8b2981044362478c04 Mon Sep 17 00:00:00 2001 From: Pete Bentley Date: Sun, 21 Feb 2021 18:26:28 +0000 Subject: Upgrade external/junit to 4.13.2 Contains just the changes from 4.12 to 4.13.2 and undoes local Android changes. Will re-patch those in in subsequent CLs. This change re-lands https://r.android.com/1598413. Bug: 129054170 Test: m Change-Id: I6135799c8be5db2ec4c3f13951c18c072427e30d --- .../java/org/junit/rules/ExpectedException.java | 57 ++++++++++++++-------- 1 file changed, 36 insertions(+), 21 deletions(-) (limited to 'src/main/java/org/junit/rules/ExpectedException.java') diff --git a/src/main/java/org/junit/rules/ExpectedException.java b/src/main/java/org/junit/rules/ExpectedException.java index 4d61712..431ad49 100644 --- a/src/main/java/org/junit/rules/ExpectedException.java +++ b/src/main/java/org/junit/rules/ExpectedException.java @@ -7,7 +7,6 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause; import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; - import org.hamcrest.Matcher; import org.hamcrest.StringDescription; import org.junit.AssumptionViolatedException; @@ -21,7 +20,7 @@ import org.junit.runners.model.Statement; * *
 public class SimpleExpectedExceptionTest {
  *     @Rule
- *     public ExpectedException thrown= ExpectedException.none();
+ *     public ExpectedException thrown = ExpectedException.none();
  *
  *     @Test
  *     public void throwsNothing() {
@@ -35,16 +34,19 @@ import org.junit.runners.model.Statement;
  *     }
  * }
* - *

- * You have to add the {@code ExpectedException} rule to your test. + *

You have to add the {@code ExpectedException} rule to your test. * This doesn't affect your existing tests (see {@code throwsNothing()}). - * After specifiying the type of the expected exception your test is + * After specifying the type of the expected exception your test is * successful when such an exception is thrown and it fails if a * different or no exception is thrown. * - *

- * Instead of specifying the exception's type you can characterize the - * expected exception based on other criterias, too: + *

This rule does not perform any special magic to make execution continue + * as if the exception had not been thrown. So it is nearly always a mistake + * for a test method to have statements after the one that is expected to + * throw the exception. + * + *

Instead of specifying the exception's type you can characterize the + * expected exception based on other criteria, too: * *

* - *

- * You can combine any of the presented expect-methods. The test is + *

You can combine any of the presented expect-methods. The test is * successful if all specifications are met. *

 @Test
  * public void throwsException() {
@@ -63,9 +64,15 @@ import org.junit.runners.model.Statement;
  *     throw new NullPointerException("What happened?");
  * }
* + *

It is recommended to set the {@link org.junit.Rule#order() order} of the + * {@code ExpectedException} to {@code Integer.MAX_VALUE} if it is used together + * with another rule that handles exceptions, e.g. {@link ErrorCollector}. + * Otherwise failing tests may be successful. + *

 @Rule(order = Integer.MAX_VALUE)
+ * public ExpectedException thrown = ExpectedException.none();
+ * *

AssumptionViolatedExceptions

- *

- * JUnit uses {@link AssumptionViolatedException}s for indicating that a test + *

JUnit uses {@link AssumptionViolatedException}s for indicating that a test * provides no useful information. (See {@link org.junit.Assume} for more * information.) You have to call {@code assume} methods before you set * expectations of the {@code ExpectedException} rule. In this case the rule @@ -80,8 +87,7 @@ import org.junit.runners.model.Statement; * *

AssertionErrors

* - *

- * JUnit uses {@link AssertionError}s for indicating that a test is failing. You + *

JUnit uses {@link AssertionError}s for indicating that a test is failing. You * have to call {@code assert} methods before you set expectations of the * {@code ExpectedException} rule, if they should be handled by the framework. * E.g. the following test fails because of the {@code assertTrue} statement. @@ -93,8 +99,7 @@ import org.junit.runners.model.Statement; * } * *

Missing Exceptions

- *

- * By default missing exceptions are reported with an error message + *

By default missing exceptions are reported with an error message * like "Expected test to throw an instance of foo". You can configure a different * message by means of {@link #reportMissingExceptionWithMessage(String)}. You * can use a {@code %s} placeholder for the description of the expected @@ -107,7 +112,13 @@ public class ExpectedException implements TestRule { /** * Returns a {@linkplain TestRule rule} that expects no exception to * be thrown (identical to behavior without this rule). + * + * @deprecated Since 4.13 + * {@link org.junit.Assert#assertThrows(Class, org.junit.function.ThrowingRunnable) + * Assert.assertThrows} can be used to verify that your code throws a specific + * exception. */ + @Deprecated public static ExpectedException none() { return new ExpectedException(); } @@ -222,10 +233,18 @@ public class ExpectedException implements TestRule { * throw new IllegalArgumentException("What happened?", cause); * } */ - public void expectCause(Matcher expectedCause) { + public void expectCause(Matcher expectedCause) { expect(hasCause(expectedCause)); } + /** + * Check if any Exception is expected. + * @since 4.13 + */ + public final boolean isAnyExceptionExpected() { + return matcherBuilder.expectsThrowable(); + } + private class ExpectedExceptionStatement extends Statement { private final Statement next; @@ -255,10 +274,6 @@ public class ExpectedException implements TestRule { } } - private boolean isAnyExceptionExpected() { - return matcherBuilder.expectsThrowable(); - } - private void failDueToMissingException() throws AssertionError { fail(missingExceptionMessage()); } -- cgit v1.2.3