From fdfbed1985640327ebd6d9ed9663ef884bf95712 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Tue, 11 Jun 2024 09:08:48 -0700 Subject: [PATCH] Migrate some tests and testing infrastructure from `AssertionFailedError` to `AssertionError`. This is a baby step away from JUnit 3, but my actual motivation was to further take advantage of `AssertionError(String, Throwable)`, which we can now use because it was [added in API Level 19](https://developer.android.com/reference/java/lang/AssertionError#AssertionError(java.lang.String,%20java.lang.Throwable)). RELNOTES=`testing`: Some test libraries now throw `AssertionError` (instead of the more specific `AssertionFailedError`) in some cases. PiperOrigin-RevId: 642289963 --- .../testing/AbstractIteratorTester.java | 11 +++--- .../common/collect/testing/Helpers.java | 23 ++++-------- .../ReserializingTestCollectionGenerator.java | 3 +- .../testing/AbstractPackageSanityTests.java | 7 +--- .../common/testing/ClassSanityTester.java | 24 ++++-------- .../common/testing/NullPointerTester.java | 25 ++++++------- .../collect/testing/IteratorTesterTest.java | 13 +++---- .../common/graph/PackageSanityTests.java | 3 +- .../util/concurrent/AbstractFutureTest.java | 7 +--- .../common/util/concurrent/TestThread.java | 6 +-- .../testing/AbstractIteratorTester.java | 11 +++--- .../common/collect/testing/Helpers.java | 23 ++++-------- .../ReserializingTestCollectionGenerator.java | 3 +- .../testing/AbstractPackageSanityTests.java | 7 +--- .../common/testing/ClassSanityTester.java | 24 ++++-------- .../common/testing/NullPointerTester.java | 25 ++++++------- .../collect/testing/IteratorTesterTest.java | 13 +++---- .../common/testing/ClassSanityTesterTest.java | 37 +++++++++---------- .../common/testing/NullPointerTesterTest.java | 27 +++++++------- .../common/graph/PackageSanityTests.java | 3 +- .../util/concurrent/AbstractFutureTest.java | 7 +--- .../common/util/concurrent/TestThread.java | 6 +-- 22 files changed, 121 insertions(+), 187 deletions(-) diff --git a/android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java b/android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java index 377da08d56c9..92f89a4c201d 100644 --- a/android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java +++ b/android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java @@ -30,7 +30,6 @@ import java.util.NoSuchElementException; import java.util.Set; import java.util.Stack; -import junit.framework.AssertionFailedError; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -99,7 +98,7 @@ void assertPermitted(Exception exception) { + exception.getClass().getSimpleName() + " was thrown; expected " + getMessage(); - Helpers.fail(exception, message); + throw new AssertionError(message, exception); } } @@ -355,8 +354,8 @@ private void compareResultsForThisListOfStimuli() { try { stimuli[i].executeAndCompare(reference, target); verify(reference.getElements()); - } catch (AssertionFailedError cause) { - Helpers.fail(cause, "failed with stimuli " + subListCopy(stimuli, i + 1)); + } catch (AssertionError cause) { + throw new AssertionError("failed with stimuli " + subListCopy(stimuli, i + 1), cause); } } } @@ -425,12 +424,12 @@ private > void internalExecuteAndCompare( } catch (PermittedMetaException e) { referenceException = e; } catch (UnknownElementException e) { - Helpers.fail(e, e.getMessage()); + throw new AssertionError(e); } if (referenceException == null) { if (targetException != null) { - Helpers.fail(targetException, "Target threw exception when reference did not"); + throw new AssertionError("Target threw exception when reference did not", targetException); } /* diff --git a/android/guava-testlib/src/com/google/common/collect/testing/Helpers.java b/android/guava-testlib/src/com/google/common/collect/testing/Helpers.java index 9d577ecb0fe5..9049756a0cb9 100644 --- a/android/guava-testlib/src/com/google/common/collect/testing/Helpers.java +++ b/android/guava-testlib/src/com/google/common/collect/testing/Helpers.java @@ -20,6 +20,7 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -40,8 +41,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import junit.framework.Assert; -import junit.framework.AssertionFailedError; import org.checkerframework.checker.nullness.qual.Nullable; @GwtCompatible(emulated = true) @@ -88,13 +87,13 @@ private static boolean isEmpty(Iterable iterable) { public static void assertEmpty(Iterable iterable) { if (!isEmpty(iterable)) { - Assert.fail("Not true that " + iterable + " is empty"); + fail("Not true that " + iterable + " is empty"); } } public static void assertEmpty(Map map) { if (!map.isEmpty()) { - Assert.fail("Not true that " + map + " is empty"); + fail("Not true that " + map + " is empty"); } } @@ -104,7 +103,7 @@ public static void assertEqualInOrder(Iterable expected, Iterable actual) while (expectedIter.hasNext() && actualIter.hasNext()) { if (!equal(expectedIter.next(), actualIter.next())) { - Assert.fail( + fail( "contents were not equal and in the same order: " + "expected = " + expected @@ -115,7 +114,7 @@ public static void assertEqualInOrder(Iterable expected, Iterable actual) if (expectedIter.hasNext() || actualIter.hasNext()) { // actual either had too few or too many elements - Assert.fail( + fail( "contents were not equal and in the same order: " + "expected = " + expected @@ -139,7 +138,7 @@ public static void assertEqualIgnoringOrder(Iterable expected, Iterable ac // Yeah it's n^2. for (Object object : exp) { if (!act.remove(object)) { - Assert.fail( + fail( "did not contain expected element " + object + ", " @@ -170,7 +169,7 @@ public static void assertContains(Iterable actual, Object expected) { } if (!contained) { - Assert.fail("Not true that " + actual + " contains " + expected); + fail("Not true that " + actual + " contains " + expected); } } @@ -182,7 +181,7 @@ public static void assertContainsAllOf(Iterable actual, Object... expected) { } if (!expectedList.isEmpty()) { - Assert.fail("Not true that " + actual + " contains all of " + Arrays.asList(expected)); + fail("Not true that " + actual + " contains all of " + Arrays.asList(expected)); } } @@ -252,12 +251,6 @@ public void remove() { return iterator.next(); } - static void fail(Throwable cause, Object message) { - AssertionFailedError assertionFailedError = new AssertionFailedError(String.valueOf(message)); - assertionFailedError.initCause(cause); - throw assertionFailedError; - } - private static class EntryComparator implements Comparator> { final @Nullable Comparator keyComparator; diff --git a/android/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java b/android/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java index 923f56fd45c0..b1e58aaac677 100644 --- a/android/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java +++ b/android/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java @@ -59,9 +59,8 @@ static T reserialize(T object) { ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { - Helpers.fail(e, e.getMessage()); + throw new AssertionError(e); } - throw new AssertionError("not reachable"); } @Override diff --git a/android/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java b/android/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java index 67179415db57..127540ecf2bb 100644 --- a/android/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java +++ b/android/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java @@ -43,7 +43,6 @@ import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.junit.Test; @@ -307,7 +306,7 @@ protected final void ignoreClasses(Predicate> condition) { this.classFilter = and(this.classFilter, not(condition)); } - private static AssertionFailedError sanityError( + private static AssertionError sanityError( Class cls, List explicitTestNames, String description, Throwable e) { String message = String.format( @@ -318,9 +317,7 @@ private static AssertionFailedError sanityError( cls, explicitTestNames.get(0), cls.getName()); - AssertionFailedError error = new AssertionFailedError(message); - error.initCause(e); - return error; + return new AssertionError(message, e); } /** diff --git a/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java b/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java index 00b9b80254b0..dba1335df52c 100644 --- a/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java +++ b/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java @@ -456,10 +456,7 @@ public FactoryMethodReturnValueTester testNulls() throws Exception { try { nullPointerTester.testAllPublicInstanceMethods(instance); } catch (AssertionError e) { - AssertionError error = - new AssertionFailedError("Null check failed on return value of " + factory); - error.initCause(e); - throw error; + throw new AssertionError("Null check failed on return value of " + factory, e); } } } @@ -505,10 +502,8 @@ public FactoryMethodReturnValueTester testSerializable() throws Exception { try { SerializableTester.reserialize(instance); } catch (Exception e) { // sneaky checked exception - AssertionError error = - new AssertionFailedError("Serialization failed on return value of " + factory); - error.initCause(e.getCause()); - throw error; + throw new AssertionError( + "Serialization failed on return value of " + factory, e.getCause()); } } } @@ -537,16 +532,11 @@ public FactoryMethodReturnValueTester testEqualsAndSerializable() throws Excepti try { SerializableTester.reserializeAndAssert(instance); } catch (Exception e) { // sneaky checked exception - AssertionError error = - new AssertionFailedError("Serialization failed on return value of " + factory); - error.initCause(e.getCause()); - throw error; + throw new AssertionError( + "Serialization failed on return value of " + factory, e.getCause()); } catch (AssertionFailedError e) { - AssertionError error = - new AssertionFailedError( - "Return value of " + factory + " reserialized to an unequal value"); - error.initCause(e); - throw error; + throw new AssertionError( + "Return value of " + factory + " reserialized to an unequal value", e); } } } diff --git a/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java b/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java index d0202b47152f..31d1938a5d54 100644 --- a/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java +++ b/android/guava-testlib/src/com/google/common/testing/NullPointerTester.java @@ -46,7 +46,6 @@ import java.util.List; import java.util.concurrent.ConcurrentMap; import junit.framework.Assert; -import junit.framework.AssertionFailedError; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -377,19 +376,17 @@ private void testParameter( if (policy.isExpectedType(cause)) { return; } - AssertionFailedError error = - new AssertionFailedError( - String.format( - "wrong exception thrown from %s when passing null to %s parameter at index %s.%n" - + "Full parameters: %s%n" - + "Actual exception message: %s", - invokable, - invokable.getParameters().get(paramIndex).getType(), - paramIndex, - Arrays.toString(params), - cause)); - error.initCause(cause); - throw error; + throw new AssertionError( + String.format( + "wrong exception thrown from %s when passing null to %s parameter at index %s.%n" + + "Full parameters: %s%n" + + "Actual exception message: %s", + invokable, + invokable.getParameters().get(paramIndex).getType(), + paramIndex, + Arrays.toString(params), + cause), + cause); } catch (IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/android/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java b/android/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java index b44811e33f63..a15bd87028cd 100644 --- a/android/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java +++ b/android/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java @@ -26,7 +26,6 @@ import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; /** @@ -149,7 +148,7 @@ protected Iterator newTargetIterator() { return new IteratorWithSunJavaBug6529795<>(iterator); } }.test(); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { return; } fail("Should have caught jdk6 bug in target iterator"); @@ -201,18 +200,18 @@ protected Iterator newTargetIterator() { @Override protected void verify(List elements) { - throw new AssertionFailedError(message); + throw new AssertionError(message); } }; - AssertionFailedError actual = null; + AssertionError actual = null; try { tester.test(); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { actual = e; } assertNotNull("verify() should be able to cause test failure", actual); assertTrue( - "AssertionFailedError should have info about why test failed", + "AssertionError should have info about why test failed", actual.getCause().getMessage().contains(message)); } @@ -323,7 +322,7 @@ public boolean hasNext() { private static void assertFailure(IteratorTester tester) { try { tester.test(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); diff --git a/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java b/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java index 4c863020e396..97753f7ab671 100644 --- a/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java +++ b/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Truth.assertWithMessage; import com.google.common.testing.AbstractPackageSanityTests; -import junit.framework.AssertionFailedError; /** * Covers basic sanity checks for the entire package. @@ -68,7 +67,7 @@ public PackageSanityTests() { public void testNulls() throws Exception { try { super.testNulls(); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { assertWithMessage("Method did not throw null pointer OR element not in graph exception.") .that(e) .hasCauseThat() diff --git a/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java b/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java index c5c4b05795d3..b9b02edd20cc 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java @@ -1208,11 +1208,8 @@ private static int findStackFrame(ExecutionException e, String clazz, String met return i; } } - AssertionFailedError failure = - new AssertionFailedError( - "Expected element " + clazz + "." + method + " not found in stack trace"); - failure.initCause(e); - throw failure; + throw new AssertionError( + "Expected element " + clazz + "." + method + " not found in stack trace", e); } private ExecutionException getExpectingExecutionException(AbstractFuture future) diff --git a/android/guava-tests/test/com/google/common/util/concurrent/TestThread.java b/android/guava-tests/test/com/google/common/util/concurrent/TestThread.java index ef3b27410d40..49bb7792ece1 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/TestThread.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/TestThread.java @@ -77,9 +77,7 @@ public void tearDown() throws Exception { join(); if (uncaughtThrowable != null) { - throw (AssertionFailedError) - new AssertionFailedError("Uncaught throwable in " + getName()) - .initCause(uncaughtThrowable); + throw new AssertionError("Uncaught throwable in " + getName(), uncaughtThrowable); } } @@ -283,7 +281,7 @@ private static class Response { Object getResult() { if (throwable != null) { - throw (AssertionFailedError) new AssertionFailedError().initCause(throwable); + throw new AssertionError(throwable); } return result; } diff --git a/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java b/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java index 2ad9676c6251..ec0a713b7fc1 100644 --- a/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java +++ b/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java @@ -30,7 +30,6 @@ import java.util.NoSuchElementException; import java.util.Set; import java.util.Stack; -import junit.framework.AssertionFailedError; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -99,7 +98,7 @@ void assertPermitted(Exception exception) { + exception.getClass().getSimpleName() + " was thrown; expected " + getMessage(); - Helpers.fail(exception, message); + throw new AssertionError(message, exception); } } @@ -371,8 +370,8 @@ private void compareResultsForThisListOfStimuli() { try { stimuli[i].executeAndCompare(reference, target); verify(reference.getElements()); - } catch (AssertionFailedError cause) { - Helpers.fail(cause, "failed with stimuli " + subListCopy(stimuli, i + 1)); + } catch (AssertionError cause) { + throw new AssertionError("failed with stimuli " + subListCopy(stimuli, i + 1), cause); } } } @@ -441,12 +440,12 @@ private > void internalExecuteAndCompare( } catch (PermittedMetaException e) { referenceException = e; } catch (UnknownElementException e) { - Helpers.fail(e, e.getMessage()); + throw new AssertionError(e); } if (referenceException == null) { if (targetException != null) { - Helpers.fail(targetException, "Target threw exception when reference did not"); + throw new AssertionError("Target threw exception when reference did not", targetException); } /* diff --git a/guava-testlib/src/com/google/common/collect/testing/Helpers.java b/guava-testlib/src/com/google/common/collect/testing/Helpers.java index 9d577ecb0fe5..9049756a0cb9 100644 --- a/guava-testlib/src/com/google/common/collect/testing/Helpers.java +++ b/guava-testlib/src/com/google/common/collect/testing/Helpers.java @@ -20,6 +20,7 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -40,8 +41,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import junit.framework.Assert; -import junit.framework.AssertionFailedError; import org.checkerframework.checker.nullness.qual.Nullable; @GwtCompatible(emulated = true) @@ -88,13 +87,13 @@ private static boolean isEmpty(Iterable iterable) { public static void assertEmpty(Iterable iterable) { if (!isEmpty(iterable)) { - Assert.fail("Not true that " + iterable + " is empty"); + fail("Not true that " + iterable + " is empty"); } } public static void assertEmpty(Map map) { if (!map.isEmpty()) { - Assert.fail("Not true that " + map + " is empty"); + fail("Not true that " + map + " is empty"); } } @@ -104,7 +103,7 @@ public static void assertEqualInOrder(Iterable expected, Iterable actual) while (expectedIter.hasNext() && actualIter.hasNext()) { if (!equal(expectedIter.next(), actualIter.next())) { - Assert.fail( + fail( "contents were not equal and in the same order: " + "expected = " + expected @@ -115,7 +114,7 @@ public static void assertEqualInOrder(Iterable expected, Iterable actual) if (expectedIter.hasNext() || actualIter.hasNext()) { // actual either had too few or too many elements - Assert.fail( + fail( "contents were not equal and in the same order: " + "expected = " + expected @@ -139,7 +138,7 @@ public static void assertEqualIgnoringOrder(Iterable expected, Iterable ac // Yeah it's n^2. for (Object object : exp) { if (!act.remove(object)) { - Assert.fail( + fail( "did not contain expected element " + object + ", " @@ -170,7 +169,7 @@ public static void assertContains(Iterable actual, Object expected) { } if (!contained) { - Assert.fail("Not true that " + actual + " contains " + expected); + fail("Not true that " + actual + " contains " + expected); } } @@ -182,7 +181,7 @@ public static void assertContainsAllOf(Iterable actual, Object... expected) { } if (!expectedList.isEmpty()) { - Assert.fail("Not true that " + actual + " contains all of " + Arrays.asList(expected)); + fail("Not true that " + actual + " contains all of " + Arrays.asList(expected)); } } @@ -252,12 +251,6 @@ public void remove() { return iterator.next(); } - static void fail(Throwable cause, Object message) { - AssertionFailedError assertionFailedError = new AssertionFailedError(String.valueOf(message)); - assertionFailedError.initCause(cause); - throw assertionFailedError; - } - private static class EntryComparator implements Comparator> { final @Nullable Comparator keyComparator; diff --git a/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java b/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java index 923f56fd45c0..b1e58aaac677 100644 --- a/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java +++ b/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java @@ -59,9 +59,8 @@ static T reserialize(T object) { ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { - Helpers.fail(e, e.getMessage()); + throw new AssertionError(e); } - throw new AssertionError("not reachable"); } @Override diff --git a/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java b/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java index 1836b9dedf69..858e581fa39b 100644 --- a/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java +++ b/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java @@ -43,7 +43,6 @@ import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.junit.Test; @@ -308,7 +307,7 @@ protected final void ignoreClasses(Predicate> condition) { this.classFilter = and(this.classFilter, not(condition)); } - private static AssertionFailedError sanityError( + private static AssertionError sanityError( Class cls, List explicitTestNames, String description, Throwable e) { String message = String.format( @@ -319,9 +318,7 @@ private static AssertionFailedError sanityError( cls, explicitTestNames.get(0), cls.getName()); - AssertionFailedError error = new AssertionFailedError(message); - error.initCause(e); - return error; + return new AssertionError(message, e); } /** diff --git a/guava-testlib/src/com/google/common/testing/ClassSanityTester.java b/guava-testlib/src/com/google/common/testing/ClassSanityTester.java index 163fd3d20c81..3ed1fc08d14e 100644 --- a/guava-testlib/src/com/google/common/testing/ClassSanityTester.java +++ b/guava-testlib/src/com/google/common/testing/ClassSanityTester.java @@ -456,10 +456,7 @@ public FactoryMethodReturnValueTester testNulls() throws Exception { try { nullPointerTester.testAllPublicInstanceMethods(instance); } catch (AssertionError e) { - AssertionError error = - new AssertionFailedError("Null check failed on return value of " + factory); - error.initCause(e); - throw error; + throw new AssertionError("Null check failed on return value of " + factory, e); } } } @@ -505,10 +502,8 @@ public FactoryMethodReturnValueTester testSerializable() throws Exception { try { SerializableTester.reserialize(instance); } catch (Exception e) { // sneaky checked exception - AssertionError error = - new AssertionFailedError("Serialization failed on return value of " + factory); - error.initCause(e.getCause()); - throw error; + throw new AssertionError( + "Serialization failed on return value of " + factory, e.getCause()); } } } @@ -537,16 +532,11 @@ public FactoryMethodReturnValueTester testEqualsAndSerializable() throws Excepti try { SerializableTester.reserializeAndAssert(instance); } catch (Exception e) { // sneaky checked exception - AssertionError error = - new AssertionFailedError("Serialization failed on return value of " + factory); - error.initCause(e.getCause()); - throw error; + throw new AssertionError( + "Serialization failed on return value of " + factory, e.getCause()); } catch (AssertionFailedError e) { - AssertionError error = - new AssertionFailedError( - "Return value of " + factory + " reserialized to an unequal value"); - error.initCause(e); - throw error; + throw new AssertionError( + "Return value of " + factory + " reserialized to an unequal value", e); } } } diff --git a/guava-testlib/src/com/google/common/testing/NullPointerTester.java b/guava-testlib/src/com/google/common/testing/NullPointerTester.java index 32e29d43e25d..8b2ac49633c5 100644 --- a/guava-testlib/src/com/google/common/testing/NullPointerTester.java +++ b/guava-testlib/src/com/google/common/testing/NullPointerTester.java @@ -48,7 +48,6 @@ import java.util.List; import java.util.concurrent.ConcurrentMap; import junit.framework.Assert; -import junit.framework.AssertionFailedError; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -373,19 +372,17 @@ private void testParameter( if (policy.isExpectedType(cause)) { return; } - AssertionFailedError error = - new AssertionFailedError( - String.format( - "wrong exception thrown from %s when passing null to %s parameter at index %s.%n" - + "Full parameters: %s%n" - + "Actual exception message: %s", - invokable, - invokable.getParameters().get(paramIndex).getType(), - paramIndex, - Arrays.toString(params), - cause)); - error.initCause(cause); - throw error; + throw new AssertionError( + String.format( + "wrong exception thrown from %s when passing null to %s parameter at index %s.%n" + + "Full parameters: %s%n" + + "Actual exception message: %s", + invokable, + invokable.getParameters().get(paramIndex).getType(), + paramIndex, + Arrays.toString(params), + cause), + cause); } catch (IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java b/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java index b44811e33f63..a15bd87028cd 100644 --- a/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java +++ b/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java @@ -26,7 +26,6 @@ import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; /** @@ -149,7 +148,7 @@ protected Iterator newTargetIterator() { return new IteratorWithSunJavaBug6529795<>(iterator); } }.test(); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { return; } fail("Should have caught jdk6 bug in target iterator"); @@ -201,18 +200,18 @@ protected Iterator newTargetIterator() { @Override protected void verify(List elements) { - throw new AssertionFailedError(message); + throw new AssertionError(message); } }; - AssertionFailedError actual = null; + AssertionError actual = null; try { tester.test(); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { actual = e; } assertNotNull("verify() should be able to cause test failure", actual); assertTrue( - "AssertionFailedError should have info about why test failed", + "AssertionError should have info about why test failed", actual.getCause().getMessage().contains(message)); } @@ -323,7 +322,7 @@ public boolean hasNext() { private static void assertFailure(IteratorTester tester) { try { tester.test(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); diff --git a/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java b/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java index a6378bea4a1f..9cc625f2b02d 100644 --- a/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java @@ -37,7 +37,6 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.checkerframework.checker.nullness.qual.Nullable; @@ -89,7 +88,7 @@ static Object badButNotPublic() { public void testForAllPublicStaticMethods_noPublicStaticMethods() throws Exception { try { tester.forAllPublicStaticMethods(NoPublicStaticMethods.class).testEquals(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected) .hasMessageThat() .isEqualTo( @@ -104,7 +103,7 @@ public void testForAllPublicStaticMethods_noPublicStaticMethods() throws Excepti public void testEqualsOnReturnValues_bad() throws Exception { try { tester.forAllPublicStaticMethods(BadEqualsFactory.class).testEquals(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -132,7 +131,7 @@ public static Object good(String s) { public void testNullsOnReturnValues_bad() throws Exception { try { tester.forAllPublicStaticMethods(BadNullsFactory.class).thatReturn(Object.class).testNulls(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -144,7 +143,7 @@ public void testNullsOnReturnValues_returnTypeFiltered() throws Exception { .forAllPublicStaticMethods(BadNullsFactory.class) .thatReturn(Iterable.class) .testNulls(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected) .hasMessageThat() .isEqualTo( @@ -180,7 +179,7 @@ public static Object good(AnInterface i) { public void testSerializableOnReturnValues_bad() throws Exception { try { tester.forAllPublicStaticMethods(BadSerializableFactory.class).testSerializable(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -199,7 +198,7 @@ public void testEqualsAndSerializableOnReturnValues_equalsIsGoodButNotSerializab throws Exception { try { tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEqualsAndSerializable(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("should have failed"); @@ -208,7 +207,7 @@ public void testEqualsAndSerializableOnReturnValues_equalsIsGoodButNotSerializab public void testEqualsAndSerializableOnReturnValues_serializableButNotEquals() throws Exception { try { tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testEqualsAndSerializable(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("should have failed"); @@ -230,7 +229,7 @@ public static Object good(AnInterface s) { public void testEqualsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception { try { tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class).testEquals(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -239,7 +238,7 @@ public void testEqualsForReturnValues_factoryReturnsNullButNotAnnotated() throws public void testNullsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception { try { tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class).testNulls(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -250,7 +249,7 @@ public void testSerializableForReturnValues_factoryReturnsNullButNotAnnotated() tester .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) .testSerializable(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -262,7 +261,7 @@ public void testEqualsAndSerializableForReturnValues_factoryReturnsNullButNotAnn tester .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) .testEqualsAndSerializable(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -318,7 +317,7 @@ public void testEquals_enum() { public void testBadEquals() throws Exception { try { tester.testEquals(BadEquals.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected).hasMessageThat().contains("create(null)"); return; } @@ -328,7 +327,7 @@ public void testBadEquals() throws Exception { public void testBadEquals_withParameterizedType() throws Exception { try { tester.testEquals(BadEqualsWithParameterizedType.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected).hasMessageThat().contains("create([[1]])"); return; } @@ -369,7 +368,7 @@ public void testEqualsUsingReferentialEquality() throws Exception { private void assertBadUseOfReferentialEquality(Class cls) throws Exception { try { tester.testEquals(cls); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected).hasMessageThat().contains(cls.getSimpleName() + "("); return; } @@ -402,7 +401,7 @@ public void testFactoryMethodReturnsNullForEqualsTest() throws Exception { public void testFactoryMethodReturnsNullButNotAnnotatedInEqualsTest() throws Exception { try { tester.testEquals(FactoryMethodReturnsNullButNotAnnotated.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("should have failed"); @@ -451,7 +450,7 @@ public void testNulls_parameterOptionalNotInstantiable() throws Exception { public void testEnumFailsToCheckNull() throws Exception { try { tester.testNulls(EnumFailsToCheckNull.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("should have failed"); @@ -468,7 +467,7 @@ public void testNoNullChecksOnAnnotation() throws Exception { public void testBadNulls() throws Exception { try { tester.testNulls(BadNulls.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("should have failed"); @@ -478,7 +477,7 @@ public void testInstantiate_factoryMethodReturnsNullButNotAnnotated() throws Exc try { FactoryMethodReturnsNullButNotAnnotated unused = tester.instantiate(FactoryMethodReturnsNullButNotAnnotated.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected).hasMessageThat().contains("@Nullable"); return; } diff --git a/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java b/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java index c224d8a5469a..573e296485c3 100644 --- a/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java @@ -46,7 +46,6 @@ import java.util.Map; import java.util.Set; import java.util.SortedSet; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.checkerframework.checker.nullness.qual.Nullable; @@ -260,7 +259,7 @@ public void testDontAcceptIae() { tester.testAllPublicStaticMethods(ThrowsUoe.class); try { tester.testAllPublicStaticMethods(ThrowsIae.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail(); @@ -271,7 +270,7 @@ public void testStaticOneArgMethodsThatShouldPass() throws Exception { Method method = OneArg.class.getMethod(methodName, String.class); try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); - } catch (AssertionFailedError unexpected) { + } catch (AssertionError unexpected) { fail("Should not have flagged method " + methodName); } } @@ -283,7 +282,7 @@ public void testStaticOneArgMethodsThatShouldFail() throws Exception { boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { foundProblem = true; } assertTrue("Should report error in method " + methodName, foundProblem); @@ -296,7 +295,7 @@ public void testNonStaticOneArgMethodsThatShouldPass() throws Exception { Method method = OneArg.class.getMethod(methodName, String.class); try { new NullPointerTester().testMethodParameter(foo, method, 0); - } catch (AssertionFailedError unexpected) { + } catch (AssertionError unexpected) { fail("Should not have flagged method " + methodName); } } @@ -309,7 +308,7 @@ public void testNonStaticOneArgMethodsThatShouldFail() throws Exception { boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(foo, method, 0); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { foundProblem = true; } assertTrue("Should report error in method " + methodName, foundProblem); @@ -321,7 +320,7 @@ public void testMessageOtherException() throws Exception { boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected.getMessage()).contains("index 0"); assertThat(expected.getMessage()).contains("[null]"); foundProblem = true; @@ -334,7 +333,7 @@ public void testMessageNoException() throws Exception { boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { assertThat(expected.getMessage()).contains("index 0"); assertThat(expected.getMessage()).contains("[null]"); foundProblem = true; @@ -429,7 +428,7 @@ public String toString() { public void verifyBarPass(Method method, TwoArg bar) { try { new NullPointerTester().testMethod(bar, method); - } catch (AssertionFailedError incorrectError) { + } catch (AssertionError incorrectError) { String errorMessage = rootLocaleFormat("Should not have flagged method %s for %s", method.getName(), bar); assertNull(errorMessage, incorrectError); @@ -439,7 +438,7 @@ public void verifyBarPass(Method method, TwoArg bar) { public void verifyBarFail(Method method, TwoArg bar) { try { new NullPointerTester().testMethod(bar, method); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; // good...we wanted a failure } String errorMessage = @@ -905,7 +904,7 @@ private static void shouldPass(Object instance) { private static void shouldFail(Object instance, Visibility visibility) { try { new NullPointerTester().testInstanceMethods(instance, visibility); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("Should detect problem in " + instance.getClass().getSimpleName()); @@ -920,7 +919,7 @@ private static void shouldFail(Object instance) { private static void shouldFail(Class cls, Visibility visibility) { try { new NullPointerTester().testStaticMethods(cls, visibility); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("Should detect problem in " + cls.getSimpleName()); @@ -1138,7 +1137,7 @@ public void checkArray(EmptyEnum object, String s) { void check() { try { runTester(); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("Should have failed because enum has no constant"); @@ -1481,7 +1480,7 @@ public void testConstructor_Ignored_ShouldPass() throws Exception { public void testConstructor_ShouldFail() throws Exception { try { new NullPointerTester().testAllPublicConstructors(FailOnOneOfTwoConstructors.class); - } catch (AssertionFailedError expected) { + } catch (AssertionError expected) { return; } fail("Should detect problem in " + FailOnOneOfTwoConstructors.class.getSimpleName()); diff --git a/guava-tests/test/com/google/common/graph/PackageSanityTests.java b/guava-tests/test/com/google/common/graph/PackageSanityTests.java index 4c863020e396..97753f7ab671 100644 --- a/guava-tests/test/com/google/common/graph/PackageSanityTests.java +++ b/guava-tests/test/com/google/common/graph/PackageSanityTests.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Truth.assertWithMessage; import com.google.common.testing.AbstractPackageSanityTests; -import junit.framework.AssertionFailedError; /** * Covers basic sanity checks for the entire package. @@ -68,7 +67,7 @@ public PackageSanityTests() { public void testNulls() throws Exception { try { super.testNulls(); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { assertWithMessage("Method did not throw null pointer OR element not in graph exception.") .that(e) .hasCauseThat() diff --git a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java b/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java index c5c4b05795d3..b9b02edd20cc 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java @@ -1208,11 +1208,8 @@ private static int findStackFrame(ExecutionException e, String clazz, String met return i; } } - AssertionFailedError failure = - new AssertionFailedError( - "Expected element " + clazz + "." + method + " not found in stack trace"); - failure.initCause(e); - throw failure; + throw new AssertionError( + "Expected element " + clazz + "." + method + " not found in stack trace", e); } private ExecutionException getExpectingExecutionException(AbstractFuture future) diff --git a/guava-tests/test/com/google/common/util/concurrent/TestThread.java b/guava-tests/test/com/google/common/util/concurrent/TestThread.java index ef3b27410d40..49bb7792ece1 100644 --- a/guava-tests/test/com/google/common/util/concurrent/TestThread.java +++ b/guava-tests/test/com/google/common/util/concurrent/TestThread.java @@ -77,9 +77,7 @@ public void tearDown() throws Exception { join(); if (uncaughtThrowable != null) { - throw (AssertionFailedError) - new AssertionFailedError("Uncaught throwable in " + getName()) - .initCause(uncaughtThrowable); + throw new AssertionError("Uncaught throwable in " + getName(), uncaughtThrowable); } } @@ -283,7 +281,7 @@ private static class Response { Object getResult() { if (throwable != null) { - throw (AssertionFailedError) new AssertionFailedError().initCause(throwable); + throw new AssertionError(throwable); } return result; }