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 a4a4f1d310c7..d9eb3a9c5fb2 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 @@ -18,9 +18,11 @@ import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.collect.ImmutableList; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -137,20 +139,17 @@ public void remove() { } public void testCanCatchJdkBug6529795InTargetIterator() { - try { - /* Choose 4 steps to get sequence [next, next, next, remove] */ - new IteratorTester( - 4, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) { - @Override - protected Iterator newTargetIterator() { - Iterator iterator = newArrayList(1, 2).iterator(); - return new IteratorWithJdkBug6529795<>(iterator); - } - }.test(); - } catch (AssertionError e) { - return; - } - fail("Should have caught jdk6 bug in target iterator"); + IteratorTester tester = + new IteratorTester( + // We choose 4 steps to get the sequence [next, next, next, remove]. + 4, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) { + @Override + protected Iterator newTargetIterator() { + Iterator iterator = newArrayList(1, 2).iterator(); + return new IteratorWithJdkBug6529795<>(iterator); + } + }; + assertFailure(tester); } private static final int STEPS = 3; @@ -202,13 +201,7 @@ protected void verify(List elements) { throw new AssertionError(message); } }; - AssertionError actual = null; - try { - tester.test(); - } catch (AssertionError e) { - actual = e; - } - assertNotNull("verify() should be able to cause test failure", actual); + AssertionError actual = assertFailure(tester); assertTrue( "AssertionError should have info about why test failed", actual.getCause().getMessage().contains(message)); @@ -318,13 +311,9 @@ public boolean hasNext() { assertFailure(tester); } - private static void assertFailure(IteratorTester tester) { - try { - tester.test(); - } catch (AssertionError expected) { - return; - } - fail(); + @CanIgnoreReturnValue + private static AssertionError assertFailure(IteratorTester tester) { + return assertThrows(AssertionError.class, tester::test); } private static final class ThrowingIterator implements Iterator { diff --git a/android/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java b/android/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java index 68e36786fd2d..207f9d0e37e9 100644 --- a/android/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java +++ b/android/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkState; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; -import static org.junit.Assert.fail; import com.google.common.annotations.GwtCompatible; import com.google.common.base.Equivalence; @@ -105,17 +104,15 @@ public void test_symmetric() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1, group1Item2).test(); - } catch (AssertionFailedError expected) { - assertThat(expected) - .hasMessageThat() - .contains( - "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " - + "TestObject{group=1, item=1} [group 1, item 1]"); - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1, group1Item2).test()); + assertThat(expected) + .hasMessageThat() + .contains( + "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " + + "TestObject{group=1, item=1} [group 1, item 1]"); } @Test @@ -137,17 +134,15 @@ public void test_transitive() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test(); - } catch (AssertionFailedError expected) { - assertThat(expected) - .hasMessageThat() - .contains( - "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " - + "TestObject{group=1, item=3} [group 1, item 3]"); - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test()); + assertThat(expected) + .hasMessageThat() + .contains( + "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " + + "TestObject{group=1, item=3} [group 1, item 3]"); } @Test @@ -163,17 +158,15 @@ public void test_inequivalence() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test(); - } catch (AssertionFailedError expected) { - assertThat(expected) - .hasMessageThat() - .contains( - "TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to " - + "TestObject{group=2, item=1} [group 2, item 1]"); - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test()); + assertThat(expected) + .hasMessageThat() + .contains( + "TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to " + + "TestObject{group=2, item=1} [group 2, item 1]"); } @Test @@ -189,18 +182,14 @@ public void test_hash() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1, group1Item2).test(); - } catch (AssertionFailedError expected) { - String expectedMessage = - "the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be " - + "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]"; - if (!expected.getMessage().contains(expectedMessage)) { - fail("<" + expected.getMessage() + "> expected to contain <" + expectedMessage + ">"); - } - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1, group1Item2).test()); + String expectedMessage = + "the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be " + + "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]"; + assertThat(expected).hasMessageThat().contains(expectedMessage); } /** An object with a friendly {@link #toString()}. */ diff --git a/android/guava-testlib/test/com/google/common/testing/TearDownStackTest.java b/android/guava-testlib/test/com/google/common/testing/TearDownStackTest.java index 3a4d352908aa..a9bc65de8966 100644 --- a/android/guava-testlib/test/com/google/common/testing/TearDownStackTest.java +++ b/android/guava-testlib/test/com/google/common/testing/TearDownStackTest.java @@ -19,6 +19,7 @@ import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.truth.Truth.assertThat; import static java.util.Arrays.asList; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import junit.framework.TestCase; @@ -86,15 +87,9 @@ public void testThrowingTearDown() { assertEquals(false, tearDownOne.ran); assertEquals(false, tearDownTwo.ran); - try { - stack.runTearDown(); - fail("runTearDown should have thrown an exception"); - } catch (RuntimeException expected) { - assertThat(expected).hasMessageThat().isEqualTo("two"); - assertThat(getOnlyElement(asList(expected.getSuppressed()))) - .hasMessageThat() - .isEqualTo("one"); - } + RuntimeException expected = assertThrows(RuntimeException.class, () -> stack.runTearDown()); + assertThat(expected).hasMessageThat().isEqualTo("two"); + assertThat(getOnlyElement(asList(expected.getSuppressed()))).hasMessageThat().isEqualTo("one"); assertEquals(true, tearDownOne.ran); assertEquals(true, tearDownTwo.ran); diff --git a/android/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java b/android/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java index 0885065d8e2c..b6d649ddd423 100644 --- a/android/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java +++ b/android/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java @@ -210,15 +210,13 @@ private void assertFailure( Class interfaceType, Function wrapperFunction, String... expectedMessages) { - try { - tester.testForwarding(interfaceType, wrapperFunction); - } catch (AssertionFailedError expected) { - for (String message : expectedMessages) { - assertThat(expected).hasMessageThat().contains(message); - } - return; + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.testForwarding(interfaceType, wrapperFunction)); + for (String message : expectedMessages) { + assertThat(expected).hasMessageThat().contains(message); } - fail("expected failure not reported"); } private static class ForwardingRunnable implements Runnable { diff --git a/android/guava-tests/test/com/google/common/hash/HashTestUtils.java b/android/guava-tests/test/com/google/common/hash/HashTestUtils.java index e4d044c78786..64d2812e78ec 100644 --- a/android/guava-tests/test/com/google/common/hash/HashTestUtils.java +++ b/android/guava-tests/test/com/google/common/hash/HashTestUtils.java @@ -23,6 +23,7 @@ import static java.nio.charset.StandardCharsets.US_ASCII; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Ints; @@ -533,25 +534,15 @@ static void assertHasherByteBufferPreservesByteOrder(HashFunction hashFunction) } static void assertHashBytesThrowsCorrectExceptions(HashFunction hashFunction) { - { - HashCode unused = hashFunction.hashBytes(new byte[64], 0, 0); - } - - try { - hashFunction.hashBytes(new byte[128], -1, 128); - Assert.fail(); - } catch (IndexOutOfBoundsException expected) { - } - try { - hashFunction.hashBytes(new byte[128], 64, 256 /* too long len */); - Assert.fail(); - } catch (IndexOutOfBoundsException expected) { - } - try { - hashFunction.hashBytes(new byte[64], 0, -1); - Assert.fail(); - } catch (IndexOutOfBoundsException expected) { - } + HashCode unused = hashFunction.hashBytes(new byte[64], 0, 0); + + assertThrows( + IndexOutOfBoundsException.class, () -> hashFunction.hashBytes(new byte[128], -1, 128)); + assertThrows( + IndexOutOfBoundsException.class, + () -> hashFunction.hashBytes(new byte[128], 64, 256 /* too long len */)); + assertThrows( + IndexOutOfBoundsException.class, () -> hashFunction.hashBytes(new byte[64], 0, -1)); } static void assertIndependentHashers(HashFunction hashFunction) { diff --git a/android/guava-tests/test/com/google/common/math/LongMathTest.java b/android/guava-tests/test/com/google/common/math/LongMathTest.java index 993263857ac0..812e214dca01 100644 --- a/android/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/android/guava-tests/test/com/google/common/math/LongMathTest.java @@ -304,13 +304,10 @@ public void testLog10Exact() { for (long x : POSITIVE_LONG_CANDIDATES) { int floor = LongMath.log10(x, FLOOR); boolean expectedSuccess = LongMath.pow(10, floor) == x; - try { + if (expectedSuccess) { assertThat(LongMath.log10(x, UNNECESSARY)).isEqualTo(floor); - assertThat(expectedSuccess).isTrue(); - } catch (ArithmeticException e) { - if (expectedSuccess) { - failFormat("expected log10(%s, UNNECESSARY) = %s; got ArithmeticException", x, floor); - } + } else { + assertThrows(ArithmeticException.class, () -> LongMath.log10(x, UNNECESSARY)); } } } @@ -351,11 +348,10 @@ public void testSqrtExactMatchesFloorOrThrows() { long sqrtFloor = sqrt(x, FLOOR); // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = sqrtFloor * sqrtFloor == x; - try { + if (isPerfectSquare) { assertThat(sqrt(x, UNNECESSARY)).isEqualTo(sqrtFloor); - assertThat(isPerfectSquare).isTrue(); - } catch (ArithmeticException e) { - assertThat(isPerfectSquare).isFalse(); + } else { + assertThrows(ArithmeticException.class, () -> sqrt(x, UNNECESSARY)); } } } diff --git a/android/guava-tests/test/com/google/common/primitives/BytesTest.java b/android/guava-tests/test/com/google/common/primitives/BytesTest.java index ec84bae3a033..f8836070d14f 100644 --- a/android/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -160,11 +160,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { byte[] sharedArray = new byte[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Bytes.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Bytes.concat(arrays)); } public void testEnsureCapacity() { diff --git a/android/guava-tests/test/com/google/common/primitives/CharsTest.java b/android/guava-tests/test/com/google/common/primitives/CharsTest.java index fa810bacee9d..86955755e37f 100644 --- a/android/guava-tests/test/com/google/common/primitives/CharsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/CharsTest.java @@ -84,14 +84,11 @@ public void testSaturatedCast() { } private void assertCastFails(long value) { - try { - Chars.checkedCast(value); - fail("Cast to char should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> Chars.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } // We need to test that our method behaves like the JDK method. @@ -243,11 +240,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { char[] sharedArray = new char[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Chars.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Chars.concat(arrays)); } @GwtIncompatible // Chars.fromByteArray diff --git a/android/guava-tests/test/com/google/common/primitives/DoublesTest.java b/android/guava-tests/test/com/google/common/primitives/DoublesTest.java index 49b2267e7da5..375a125ae0f4 100644 --- a/android/guava-tests/test/com/google/common/primitives/DoublesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/DoublesTest.java @@ -276,11 +276,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { double[] sharedArray = new double[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Doubles.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Doubles.concat(arrays)); } public void testEnsureCapacity() { diff --git a/android/guava-tests/test/com/google/common/primitives/FloatsTest.java b/android/guava-tests/test/com/google/common/primitives/FloatsTest.java index ebdbde7be645..a2db04649070 100644 --- a/android/guava-tests/test/com/google/common/primitives/FloatsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/FloatsTest.java @@ -269,11 +269,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { float[] sharedArray = new float[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Floats.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Floats.concat(arrays)); } public void testEnsureCapacity() { diff --git a/android/guava-tests/test/com/google/common/primitives/IntsTest.java b/android/guava-tests/test/com/google/common/primitives/IntsTest.java index b0e4881c4511..08c32a612bd0 100644 --- a/android/guava-tests/test/com/google/common/primitives/IntsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/IntsTest.java @@ -89,14 +89,11 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - Ints.checkedCast(value); - fail("Cast to int should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> Ints.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } // We need to test that our method behaves like the JDK method. @@ -246,11 +243,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { int[] sharedArray = new int[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Ints.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Ints.concat(arrays)); } public void testToByteArray() { diff --git a/android/guava-tests/test/com/google/common/primitives/LongsTest.java b/android/guava-tests/test/com/google/common/primitives/LongsTest.java index fa422a1deec0..e70103017b27 100644 --- a/android/guava-tests/test/com/google/common/primitives/LongsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/LongsTest.java @@ -196,11 +196,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { long[] sharedArray = new long[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Longs.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Longs.concat(arrays)); } private static void assertByteArrayEquals(byte[] expected, byte[] actual) { diff --git a/android/guava-tests/test/com/google/common/primitives/ShortsTest.java b/android/guava-tests/test/com/google/common/primitives/ShortsTest.java index 533a90469545..f202d8520af4 100644 --- a/android/guava-tests/test/com/google/common/primitives/ShortsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/ShortsTest.java @@ -88,14 +88,11 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - Shorts.checkedCast(value); - fail("Cast to short should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> Shorts.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } // We need to test that our method behaves like the JDK method. @@ -266,11 +263,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { short[] sharedArray = new short[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Shorts.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Shorts.concat(arrays)); } @GwtIncompatible // Shorts.toByteArray diff --git a/android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java b/android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java index 7c8c912cbe5c..abf4b706c672 100644 --- a/android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java @@ -85,14 +85,11 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - UnsignedBytes.checkedCast(value); - fail("Cast to byte should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> UnsignedBytes.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } public void testCompare() { @@ -132,19 +129,11 @@ public void testMin() { } private static void assertParseFails(String value) { - try { - UnsignedBytes.parseUnsignedByte(value); - fail(); - } catch (NumberFormatException expected) { - } + assertThrows(NumberFormatException.class, () -> UnsignedBytes.parseUnsignedByte(value)); } private static void assertParseFails(String value, int radix) { - try { - UnsignedBytes.parseUnsignedByte(value, radix); - fail(); - } catch (NumberFormatException expected) { - } + assertThrows(NumberFormatException.class, () -> UnsignedBytes.parseUnsignedByte(value, radix)); } public void testParseUnsignedByte() { diff --git a/android/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java b/android/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java index 385ee0f617af..6583e4c8c4a5 100644 --- a/android/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java @@ -68,12 +68,9 @@ public void testCheckedCast() { } private static void assertCastFails(long value) { - try { - UnsignedInts.checkedCast(value); - fail("Cast to int should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertThat(ex).hasMessageThat().contains(String.valueOf(value)); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> UnsignedInts.checkedCast(value)); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } public void testSaturatedCast() { @@ -221,11 +218,10 @@ public void testSortDescendingIndexed() { public void testDivide() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { - try { + if (b == 0) { + assertThrows(ArithmeticException.class, () -> UnsignedInts.divide((int) a, (int) b)); + } else { assertThat(UnsignedInts.divide((int) a, (int) b)).isEqualTo((int) (a / b)); - assertThat(b).isNotEqualTo(0); - } catch (ArithmeticException e) { - assertThat(b).isEqualTo(0); } } } @@ -234,11 +230,10 @@ public void testDivide() { public void testRemainder() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { - try { + if (b == 0) { + assertThrows(ArithmeticException.class, () -> UnsignedInts.remainder((int) a, (int) b)); + } else { assertThat(UnsignedInts.remainder((int) a, (int) b)).isEqualTo((int) (a % b)); - assertThat(b).isNotEqualTo(0); - } catch (ArithmeticException e) { - assertThat(b).isEqualTo(0); } } } diff --git a/android/guava-tests/test/com/google/common/reflect/TypeTokenTest.java b/android/guava-tests/test/com/google/common/reflect/TypeTokenTest.java index 45e945197953..a7438f1e55cf 100644 --- a/android/guava-tests/test/com/google/common/reflect/TypeTokenTest.java +++ b/android/guava-tests/test/com/google/common/reflect/TypeTokenTest.java @@ -343,23 +343,19 @@ public void testAssertSubtypeTokenBeforeSupertypeToken_subtypeFirst() { } public void testAssertSubtypeTokenBeforeSupertypeToken_supertypeFirst() { - try { - assertSubtypeTokenBeforeSupertypeToken( - ImmutableList.of(TypeToken.of(CharSequence.class), TypeToken.of(String.class))); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + assertSubtypeTokenBeforeSupertypeToken( + ImmutableList.of(TypeToken.of(CharSequence.class), TypeToken.of(String.class)))); } public void testAssertSubtypeTokenBeforeSupertypeToken_duplicate() { - try { - assertSubtypeTokenBeforeSupertypeToken( - ImmutableList.of(TypeToken.of(String.class), TypeToken.of(String.class))); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + assertSubtypeTokenBeforeSupertypeToken( + ImmutableList.of(TypeToken.of(String.class), TypeToken.of(String.class)))); } public void testAssertSubtypeBeforeSupertype_empty() { @@ -375,21 +371,15 @@ public void testAssertSubtypeBeforeSupertype_subtypeFirst() { } public void testAssertSubtypeBeforeSupertype_supertypeFirst() { - try { - assertSubtypeBeforeSupertype(ImmutableList.of(CharSequence.class, String.class)); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> assertSubtypeBeforeSupertype(ImmutableList.of(CharSequence.class, String.class))); } public void testAssertSubtypeBeforeSupertype_duplicate() { - try { - assertSubtypeBeforeSupertype(ImmutableList.of(String.class, String.class)); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> assertSubtypeBeforeSupertype(ImmutableList.of(String.class, String.class))); } public void testGetGenericSuperclass_noSuperclass() { @@ -1768,11 +1758,7 @@ public void testRejectTypeVariable_withOwnerType() { } private static void assertHasTypeVariable(Type type) { - try { - TypeToken.of(type).rejectTypeVariables(); - fail("Should contain TypeVariable"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> TypeToken.of(type).rejectTypeVariables()); } private static void assertNoTypeVariable(Type type) { diff --git a/android/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java b/android/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java index 6712b3cf8d29..9cbaa3ad7d4c 100644 --- a/android/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java +++ b/android/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java @@ -17,6 +17,7 @@ package com.google.common.reflect; import static com.google.common.reflect.Types.subtypeOf; +import static org.junit.Assert.assertThrows; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; @@ -103,16 +104,8 @@ void visitTypeVariable(TypeVariable t) { private static void assertVisited(Type type) { TypeVisitor visitor = new BaseTypeVisitor(); - try { - visitor.visit(type); - fail("Type not visited"); - } catch (UnsupportedOperationException expected) { - } - try { - visitor.visit(new Type[] {type}); - fail("Type not visited"); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, () -> visitor.visit(type)); + assertThrows(UnsupportedOperationException.class, () -> visitor.visit(new Type[] {type})); } private static class BaseTypeVisitor extends TypeVisitor { diff --git a/android/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java b/android/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java index ba6bf8702ef4..d71496a2d571 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java @@ -86,12 +86,8 @@ T getFinalValue(ClosingFuture closingFuture) throws ExecutionException { void assertFinallyFailsWithException(ClosingFuture closingFuture) { assertThatFutureFailsWithException(closingFuture.statusFuture()); ValueAndCloser valueAndCloser = finishToValueAndCloser(closingFuture); - try { - valueAndCloser.get(); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isEqualTo(exception); - } + ExecutionException expected = assertThrows(ExecutionException.class, valueAndCloser::get); + assertThat(expected).hasCauseThat().isEqualTo(exception); valueAndCloser.closeAsync(); } diff --git a/android/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java b/android/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java index b1fac0561880..14d2b3fb217a 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java @@ -16,6 +16,8 @@ package com.google.common.util.concurrent; +import static org.junit.Assert.assertThrows; + import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.collect.ForwardingObject; @@ -33,13 +35,13 @@ public class ForwardingObjectTesterTest extends TestCase { public void testFailsToForward() { - try { - ForwardingObjectTester.testForwardingObject(FailToForward.class); - } catch (AssertionError | UnsupportedOperationException expected) { - // UnsupportedOperationException is what we see on Android. - return; - } - fail("Should have thrown"); + Throwable expected = + assertThrows( + Throwable.class, + () -> ForwardingObjectTester.testForwardingObject(FailToForward.class)); + assertTrue( + "Expected AssertionError or UnsupportedOperationException", + expected instanceof AssertionError || expected instanceof UnsupportedOperationException); } @AndroidIncompatible // TODO(cpovirk): java.lang.IllegalAccessError: superclass not accessible diff --git a/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java b/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java index b7c749ad766e..8617c6ab0bef 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java @@ -23,7 +23,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import java.util.concurrent.ExecutionException; @@ -36,23 +36,13 @@ final class TestPlatform { static void verifyGetOnPendingFuture(Future future) { checkNotNull(future); - try { - pseudoTimedGetUninterruptibly(future, 10, MILLISECONDS); - fail(); - } catch (TimeoutException expected) { - } catch (ExecutionException e) { - throw new AssertionError(e); - } + assertThrows( + TimeoutException.class, () -> pseudoTimedGetUninterruptibly(future, 10, MILLISECONDS)); } static void verifyTimedGetOnPendingFuture(Future future) { - try { - getUninterruptibly(future, 0, SECONDS); - fail(); - } catch (TimeoutException expected) { - } catch (ExecutionException e) { - throw new AssertionError(e); - } + checkNotNull(future); + assertThrows(TimeoutException.class, () -> getUninterruptibly(future, 0, SECONDS)); } static void verifyThreadWasNotInterrupted() { 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 a4a4f1d310c7..d9eb3a9c5fb2 100644 --- a/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java +++ b/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java @@ -18,9 +18,11 @@ import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.collect.ImmutableList; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -137,20 +139,17 @@ public void remove() { } public void testCanCatchJdkBug6529795InTargetIterator() { - try { - /* Choose 4 steps to get sequence [next, next, next, remove] */ - new IteratorTester( - 4, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) { - @Override - protected Iterator newTargetIterator() { - Iterator iterator = newArrayList(1, 2).iterator(); - return new IteratorWithJdkBug6529795<>(iterator); - } - }.test(); - } catch (AssertionError e) { - return; - } - fail("Should have caught jdk6 bug in target iterator"); + IteratorTester tester = + new IteratorTester( + // We choose 4 steps to get the sequence [next, next, next, remove]. + 4, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) { + @Override + protected Iterator newTargetIterator() { + Iterator iterator = newArrayList(1, 2).iterator(); + return new IteratorWithJdkBug6529795<>(iterator); + } + }; + assertFailure(tester); } private static final int STEPS = 3; @@ -202,13 +201,7 @@ protected void verify(List elements) { throw new AssertionError(message); } }; - AssertionError actual = null; - try { - tester.test(); - } catch (AssertionError e) { - actual = e; - } - assertNotNull("verify() should be able to cause test failure", actual); + AssertionError actual = assertFailure(tester); assertTrue( "AssertionError should have info about why test failed", actual.getCause().getMessage().contains(message)); @@ -318,13 +311,9 @@ public boolean hasNext() { assertFailure(tester); } - private static void assertFailure(IteratorTester tester) { - try { - tester.test(); - } catch (AssertionError expected) { - return; - } - fail(); + @CanIgnoreReturnValue + private static AssertionError assertFailure(IteratorTester tester) { + return assertThrows(AssertionError.class, tester::test); } private static final class ThrowingIterator implements Iterator { diff --git a/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java b/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java index 9af582ff5614..26347e441482 100644 --- a/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java @@ -90,27 +90,22 @@ static Object badButNotPublic() { } public void testForAllPublicStaticMethods_noPublicStaticMethods() throws Exception { - try { - tester.forAllPublicStaticMethods(NoPublicStaticMethods.class).testEquals(); - } catch (AssertionError expected) { - assertThat(expected) - .hasMessageThat() - .isEqualTo( - "No public static methods that return java.lang.Object or subtype are found in " - + NoPublicStaticMethods.class - + "."); - return; - } - fail(); + AssertionError expected = + assertThrows( + AssertionError.class, + () -> tester.forAllPublicStaticMethods(NoPublicStaticMethods.class).testEquals()); + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "No public static methods that return java.lang.Object or subtype are found in " + + NoPublicStaticMethods.class + + "."); } public void testEqualsOnReturnValues_bad() throws Exception { - try { - tester.forAllPublicStaticMethods(BadEqualsFactory.class).testEquals(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> tester.forAllPublicStaticMethods(BadEqualsFactory.class).testEquals()); } private static class BadEqualsFactory { @@ -134,30 +129,30 @@ public static Object good(String s) { } public void testNullsOnReturnValues_bad() throws Exception { - try { - tester.forAllPublicStaticMethods(BadNullsFactory.class).thatReturn(Object.class).testNulls(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(BadNullsFactory.class) + .thatReturn(Object.class) + .testNulls()); } public void testNullsOnReturnValues_returnTypeFiltered() throws Exception { - try { - tester - .forAllPublicStaticMethods(BadNullsFactory.class) - .thatReturn(Iterable.class) - .testNulls(); - } catch (AssertionError expected) { - assertThat(expected) - .hasMessageThat() - .isEqualTo( - "No public static methods that return java.lang.Iterable or subtype are found in " - + BadNullsFactory.class - + "."); - return; - } - fail(); + AssertionError expected = + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(BadNullsFactory.class) + .thatReturn(Iterable.class) + .testNulls()); + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "No public static methods that return java.lang.Iterable or subtype are found in " + + BadNullsFactory.class + + "."); } public static final class BadNullsFactory { @@ -186,12 +181,9 @@ private GoodSerializableFactory() {} } public void testSerializableOnReturnValues_bad() throws Exception { - try { - tester.forAllPublicStaticMethods(BadSerializableFactory.class).testSerializable(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> tester.forAllPublicStaticMethods(BadSerializableFactory.class).testSerializable()); } public static final class BadSerializableFactory { @@ -206,21 +198,19 @@ private BadSerializableFactory() {} public void testEqualsAndSerializableOnReturnValues_equalsIsGoodButNotSerializable() throws Exception { - try { - tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEqualsAndSerializable(); - } catch (AssertionError expected) { - return; - } - fail("should have failed"); + assertThrows( + AssertionError.class, + () -> + tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEqualsAndSerializable()); } public void testEqualsAndSerializableOnReturnValues_serializableButNotEquals() throws Exception { - try { - tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testEqualsAndSerializable(); - } catch (AssertionError expected) { - return; - } - fail("should have failed"); + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(GoodSerializableFactory.class) + .testEqualsAndSerializable()); } @AndroidIncompatible // TODO(cpovirk): ClassNotFoundException... ClassSanityTesterTest$AnInterface @@ -239,44 +229,40 @@ private GoodEqualsAndSerializableFactory() {} } public void testEqualsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception { - try { - tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class).testEquals(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) + .testEquals()); } public void testNullsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception { - try { - tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class).testNulls(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) + .testNulls()); } public void testSerializableForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception { - try { - tester - .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) - .testSerializable(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) + .testSerializable()); } public void testEqualsAndSerializableForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception { - try { - tester - .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) - .testEqualsAndSerializable(); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + tester + .forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class) + .testEqualsAndSerializable()); } public static final class FactoryThatReturnsNullButNotAnnotated { @@ -331,23 +317,16 @@ public void testEquals_enum() { } public void testBadEquals() { - try { - tester.testEquals(BadEquals.class); - } catch (AssertionError expected) { - assertThat(expected).hasMessageThat().contains("create(null)"); - return; - } - fail("should have failed"); + AssertionError expected = + assertThrows(AssertionError.class, () -> tester.testEquals(BadEquals.class)); + assertThat(expected).hasMessageThat().contains("create(null)"); } public void testBadEquals_withParameterizedType() { - try { - tester.testEquals(BadEqualsWithParameterizedType.class); - } catch (AssertionError expected) { - assertThat(expected).hasMessageThat().contains("create([[1]])"); - return; - } - fail("should have failed"); + AssertionError expected = + assertThrows( + AssertionError.class, () -> tester.testEquals(BadEqualsWithParameterizedType.class)); + assertThat(expected).hasMessageThat().contains("create([[1]])"); } public void testBadEquals_withSingleParameterValue() { @@ -382,13 +361,8 @@ public void testEqualsUsingReferentialEquality() { } private void assertBadUseOfReferentialEquality(Class cls) { - try { - tester.testEquals(cls); - } catch (AssertionError expected) { - assertThat(expected).hasMessageThat().contains(cls.getSimpleName() + "("); - return; - } - fail("should have failed for " + cls); + AssertionError expected = assertThrows(AssertionError.class, () -> tester.testEquals(cls)); + assertThat(expected).hasMessageThat().contains(cls.getSimpleName() + "("); } public void testParameterNotInstantiableForEqualsTest() { @@ -415,12 +389,9 @@ public void testFactoryMethodReturnsNullForEqualsTest() { } public void testFactoryMethodReturnsNullButNotAnnotatedInEqualsTest() { - try { - tester.testEquals(FactoryMethodReturnsNullButNotAnnotated.class); - } catch (AssertionError expected) { - return; - } - fail("should have failed"); + assertThrows( + AssertionError.class, + () -> tester.testEquals(FactoryMethodReturnsNullButNotAnnotated.class)); } public void testNoEqualsChecksOnEnum() { @@ -464,12 +435,7 @@ public void testNulls_parameterOptionalNotInstantiable() { } public void testEnumFailsToCheckNull() { - try { - tester.testNulls(EnumFailsToCheckNull.class); - } catch (AssertionError expected) { - return; - } - fail("should have failed"); + assertThrows(AssertionError.class, () -> tester.testNulls(EnumFailsToCheckNull.class)); } public void testNoNullChecksOnInterface() { @@ -481,23 +447,15 @@ public void testNoNullChecksOnAnnotation() { } public void testBadNulls() { - try { - tester.testNulls(BadNulls.class); - } catch (AssertionError expected) { - return; - } - fail("should have failed"); + assertThrows(AssertionError.class, () -> tester.testNulls(BadNulls.class)); } public void testInstantiate_factoryMethodReturnsNullButNotAnnotated() throws Exception { - try { - FactoryMethodReturnsNullButNotAnnotated unused = - tester.instantiate(FactoryMethodReturnsNullButNotAnnotated.class); - } catch (AssertionError expected) { - assertThat(expected).hasMessageThat().contains("@Nullable"); - return; - } - fail("should have failed"); + AssertionError expected = + assertThrows( + AssertionError.class, + () -> tester.instantiate(FactoryMethodReturnsNullButNotAnnotated.class)); + assertThat(expected).hasMessageThat().contains("@Nullable"); } public void testInstantiate_factoryMethodReturnsNullAndAnnotated() { diff --git a/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java b/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java index 68e36786fd2d..207f9d0e37e9 100644 --- a/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkState; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; -import static org.junit.Assert.fail; import com.google.common.annotations.GwtCompatible; import com.google.common.base.Equivalence; @@ -105,17 +104,15 @@ public void test_symmetric() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1, group1Item2).test(); - } catch (AssertionFailedError expected) { - assertThat(expected) - .hasMessageThat() - .contains( - "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " - + "TestObject{group=1, item=1} [group 1, item 1]"); - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1, group1Item2).test()); + assertThat(expected) + .hasMessageThat() + .contains( + "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " + + "TestObject{group=1, item=1} [group 1, item 1]"); } @Test @@ -137,17 +134,15 @@ public void test_transitive() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test(); - } catch (AssertionFailedError expected) { - assertThat(expected) - .hasMessageThat() - .contains( - "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " - + "TestObject{group=1, item=3} [group 1, item 3]"); - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test()); + assertThat(expected) + .hasMessageThat() + .contains( + "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to " + + "TestObject{group=1, item=3} [group 1, item 3]"); } @Test @@ -163,17 +158,15 @@ public void test_inequivalence() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test(); - } catch (AssertionFailedError expected) { - assertThat(expected) - .hasMessageThat() - .contains( - "TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to " - + "TestObject{group=2, item=1} [group 2, item 1]"); - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test()); + assertThat(expected) + .hasMessageThat() + .contains( + "TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to " + + "TestObject{group=2, item=1} [group 2, item 1]"); } @Test @@ -189,18 +182,14 @@ public void test_hash() { equivalenceMock.replay(); - try { - tester.addEquivalenceGroup(group1Item1, group1Item2).test(); - } catch (AssertionFailedError expected) { - String expectedMessage = - "the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be " - + "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]"; - if (!expected.getMessage().contains(expectedMessage)) { - fail("<" + expected.getMessage() + "> expected to contain <" + expectedMessage + ">"); - } - return; - } - fail(); + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.addEquivalenceGroup(group1Item1, group1Item2).test()); + String expectedMessage = + "the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be " + + "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]"; + assertThat(expected).hasMessageThat().contains(expectedMessage); } /** An object with a friendly {@link #toString()}. */ diff --git a/guava-testlib/test/com/google/common/testing/TearDownStackTest.java b/guava-testlib/test/com/google/common/testing/TearDownStackTest.java index 3a4d352908aa..a9bc65de8966 100644 --- a/guava-testlib/test/com/google/common/testing/TearDownStackTest.java +++ b/guava-testlib/test/com/google/common/testing/TearDownStackTest.java @@ -19,6 +19,7 @@ import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.truth.Truth.assertThat; import static java.util.Arrays.asList; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import junit.framework.TestCase; @@ -86,15 +87,9 @@ public void testThrowingTearDown() { assertEquals(false, tearDownOne.ran); assertEquals(false, tearDownTwo.ran); - try { - stack.runTearDown(); - fail("runTearDown should have thrown an exception"); - } catch (RuntimeException expected) { - assertThat(expected).hasMessageThat().isEqualTo("two"); - assertThat(getOnlyElement(asList(expected.getSuppressed()))) - .hasMessageThat() - .isEqualTo("one"); - } + RuntimeException expected = assertThrows(RuntimeException.class, () -> stack.runTearDown()); + assertThat(expected).hasMessageThat().isEqualTo("two"); + assertThat(getOnlyElement(asList(expected.getSuppressed()))).hasMessageThat().isEqualTo("one"); assertEquals(true, tearDownOne.ran); assertEquals(true, tearDownTwo.ran); diff --git a/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java b/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java index 0885065d8e2c..b6d649ddd423 100644 --- a/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java @@ -210,15 +210,13 @@ private void assertFailure( Class interfaceType, Function wrapperFunction, String... expectedMessages) { - try { - tester.testForwarding(interfaceType, wrapperFunction); - } catch (AssertionFailedError expected) { - for (String message : expectedMessages) { - assertThat(expected).hasMessageThat().contains(message); - } - return; + AssertionFailedError expected = + assertThrows( + AssertionFailedError.class, + () -> tester.testForwarding(interfaceType, wrapperFunction)); + for (String message : expectedMessages) { + assertThat(expected).hasMessageThat().contains(message); } - fail("expected failure not reported"); } private static class ForwardingRunnable implements Runnable { diff --git a/guava-tests/test/com/google/common/hash/HashTestUtils.java b/guava-tests/test/com/google/common/hash/HashTestUtils.java index e4d044c78786..64d2812e78ec 100644 --- a/guava-tests/test/com/google/common/hash/HashTestUtils.java +++ b/guava-tests/test/com/google/common/hash/HashTestUtils.java @@ -23,6 +23,7 @@ import static java.nio.charset.StandardCharsets.US_ASCII; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Ints; @@ -533,25 +534,15 @@ static void assertHasherByteBufferPreservesByteOrder(HashFunction hashFunction) } static void assertHashBytesThrowsCorrectExceptions(HashFunction hashFunction) { - { - HashCode unused = hashFunction.hashBytes(new byte[64], 0, 0); - } - - try { - hashFunction.hashBytes(new byte[128], -1, 128); - Assert.fail(); - } catch (IndexOutOfBoundsException expected) { - } - try { - hashFunction.hashBytes(new byte[128], 64, 256 /* too long len */); - Assert.fail(); - } catch (IndexOutOfBoundsException expected) { - } - try { - hashFunction.hashBytes(new byte[64], 0, -1); - Assert.fail(); - } catch (IndexOutOfBoundsException expected) { - } + HashCode unused = hashFunction.hashBytes(new byte[64], 0, 0); + + assertThrows( + IndexOutOfBoundsException.class, () -> hashFunction.hashBytes(new byte[128], -1, 128)); + assertThrows( + IndexOutOfBoundsException.class, + () -> hashFunction.hashBytes(new byte[128], 64, 256 /* too long len */)); + assertThrows( + IndexOutOfBoundsException.class, () -> hashFunction.hashBytes(new byte[64], 0, -1)); } static void assertIndependentHashers(HashFunction hashFunction) { diff --git a/guava-tests/test/com/google/common/math/LongMathTest.java b/guava-tests/test/com/google/common/math/LongMathTest.java index 993263857ac0..812e214dca01 100644 --- a/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/guava-tests/test/com/google/common/math/LongMathTest.java @@ -304,13 +304,10 @@ public void testLog10Exact() { for (long x : POSITIVE_LONG_CANDIDATES) { int floor = LongMath.log10(x, FLOOR); boolean expectedSuccess = LongMath.pow(10, floor) == x; - try { + if (expectedSuccess) { assertThat(LongMath.log10(x, UNNECESSARY)).isEqualTo(floor); - assertThat(expectedSuccess).isTrue(); - } catch (ArithmeticException e) { - if (expectedSuccess) { - failFormat("expected log10(%s, UNNECESSARY) = %s; got ArithmeticException", x, floor); - } + } else { + assertThrows(ArithmeticException.class, () -> LongMath.log10(x, UNNECESSARY)); } } } @@ -351,11 +348,10 @@ public void testSqrtExactMatchesFloorOrThrows() { long sqrtFloor = sqrt(x, FLOOR); // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = sqrtFloor * sqrtFloor == x; - try { + if (isPerfectSquare) { assertThat(sqrt(x, UNNECESSARY)).isEqualTo(sqrtFloor); - assertThat(isPerfectSquare).isTrue(); - } catch (ArithmeticException e) { - assertThat(isPerfectSquare).isFalse(); + } else { + assertThrows(ArithmeticException.class, () -> sqrt(x, UNNECESSARY)); } } } diff --git a/guava-tests/test/com/google/common/primitives/BytesTest.java b/guava-tests/test/com/google/common/primitives/BytesTest.java index ec84bae3a033..f8836070d14f 100644 --- a/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -160,11 +160,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { byte[] sharedArray = new byte[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Bytes.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Bytes.concat(arrays)); } public void testEnsureCapacity() { diff --git a/guava-tests/test/com/google/common/primitives/CharsTest.java b/guava-tests/test/com/google/common/primitives/CharsTest.java index fa810bacee9d..86955755e37f 100644 --- a/guava-tests/test/com/google/common/primitives/CharsTest.java +++ b/guava-tests/test/com/google/common/primitives/CharsTest.java @@ -84,14 +84,11 @@ public void testSaturatedCast() { } private void assertCastFails(long value) { - try { - Chars.checkedCast(value); - fail("Cast to char should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> Chars.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } // We need to test that our method behaves like the JDK method. @@ -243,11 +240,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { char[] sharedArray = new char[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Chars.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Chars.concat(arrays)); } @GwtIncompatible // Chars.fromByteArray diff --git a/guava-tests/test/com/google/common/primitives/DoublesTest.java b/guava-tests/test/com/google/common/primitives/DoublesTest.java index 49b2267e7da5..375a125ae0f4 100644 --- a/guava-tests/test/com/google/common/primitives/DoublesTest.java +++ b/guava-tests/test/com/google/common/primitives/DoublesTest.java @@ -276,11 +276,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { double[] sharedArray = new double[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Doubles.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Doubles.concat(arrays)); } public void testEnsureCapacity() { diff --git a/guava-tests/test/com/google/common/primitives/FloatsTest.java b/guava-tests/test/com/google/common/primitives/FloatsTest.java index ebdbde7be645..a2db04649070 100644 --- a/guava-tests/test/com/google/common/primitives/FloatsTest.java +++ b/guava-tests/test/com/google/common/primitives/FloatsTest.java @@ -269,11 +269,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { float[] sharedArray = new float[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Floats.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Floats.concat(arrays)); } public void testEnsureCapacity() { diff --git a/guava-tests/test/com/google/common/primitives/IntsTest.java b/guava-tests/test/com/google/common/primitives/IntsTest.java index b0e4881c4511..08c32a612bd0 100644 --- a/guava-tests/test/com/google/common/primitives/IntsTest.java +++ b/guava-tests/test/com/google/common/primitives/IntsTest.java @@ -89,14 +89,11 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - Ints.checkedCast(value); - fail("Cast to int should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> Ints.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } // We need to test that our method behaves like the JDK method. @@ -246,11 +243,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { int[] sharedArray = new int[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Ints.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Ints.concat(arrays)); } public void testToByteArray() { diff --git a/guava-tests/test/com/google/common/primitives/LongsTest.java b/guava-tests/test/com/google/common/primitives/LongsTest.java index fa422a1deec0..e70103017b27 100644 --- a/guava-tests/test/com/google/common/primitives/LongsTest.java +++ b/guava-tests/test/com/google/common/primitives/LongsTest.java @@ -196,11 +196,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { long[] sharedArray = new long[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Longs.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Longs.concat(arrays)); } private static void assertByteArrayEquals(byte[] expected, byte[] actual) { diff --git a/guava-tests/test/com/google/common/primitives/ShortsTest.java b/guava-tests/test/com/google/common/primitives/ShortsTest.java index 533a90469545..f202d8520af4 100644 --- a/guava-tests/test/com/google/common/primitives/ShortsTest.java +++ b/guava-tests/test/com/google/common/primitives/ShortsTest.java @@ -88,14 +88,11 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - Shorts.checkedCast(value); - fail("Cast to short should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> Shorts.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } // We need to test that our method behaves like the JDK method. @@ -266,11 +263,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) { short[] sharedArray = new short[arraysDim2]; Arrays.fill(arrays, sharedArray); - try { - Shorts.concat(arrays); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Shorts.concat(arrays)); } @GwtIncompatible // Shorts.toByteArray diff --git a/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java b/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java index 53445320cf59..b00f6aab714e 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java @@ -87,14 +87,11 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - UnsignedBytes.checkedCast(value); - fail("Cast to byte should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) - .that(ex.getMessage().contains(String.valueOf(value))) - .isTrue(); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> UnsignedBytes.checkedCast(value)); + assertWithMessage("%s not found in exception text: %s", value, ex.getMessage()) + .that(ex.getMessage().contains(String.valueOf(value))) + .isTrue(); } public void testCompare() { @@ -134,19 +131,11 @@ public void testMin() { } private static void assertParseFails(String value) { - try { - UnsignedBytes.parseUnsignedByte(value); - fail(); - } catch (NumberFormatException expected) { - } + assertThrows(NumberFormatException.class, () -> UnsignedBytes.parseUnsignedByte(value)); } private static void assertParseFails(String value, int radix) { - try { - UnsignedBytes.parseUnsignedByte(value, radix); - fail(); - } catch (NumberFormatException expected) { - } + assertThrows(NumberFormatException.class, () -> UnsignedBytes.parseUnsignedByte(value, radix)); } public void testParseUnsignedByte() { diff --git a/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java b/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java index 385ee0f617af..6583e4c8c4a5 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedIntsTest.java @@ -68,12 +68,9 @@ public void testCheckedCast() { } private static void assertCastFails(long value) { - try { - UnsignedInts.checkedCast(value); - fail("Cast to int should have failed: " + value); - } catch (IllegalArgumentException ex) { - assertThat(ex).hasMessageThat().contains(String.valueOf(value)); - } + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, () -> UnsignedInts.checkedCast(value)); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } public void testSaturatedCast() { @@ -221,11 +218,10 @@ public void testSortDescendingIndexed() { public void testDivide() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { - try { + if (b == 0) { + assertThrows(ArithmeticException.class, () -> UnsignedInts.divide((int) a, (int) b)); + } else { assertThat(UnsignedInts.divide((int) a, (int) b)).isEqualTo((int) (a / b)); - assertThat(b).isNotEqualTo(0); - } catch (ArithmeticException e) { - assertThat(b).isEqualTo(0); } } } @@ -234,11 +230,10 @@ public void testDivide() { public void testRemainder() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { - try { + if (b == 0) { + assertThrows(ArithmeticException.class, () -> UnsignedInts.remainder((int) a, (int) b)); + } else { assertThat(UnsignedInts.remainder((int) a, (int) b)).isEqualTo((int) (a % b)); - assertThat(b).isNotEqualTo(0); - } catch (ArithmeticException e) { - assertThat(b).isEqualTo(0); } } } diff --git a/guava-tests/test/com/google/common/reflect/TypeTokenTest.java b/guava-tests/test/com/google/common/reflect/TypeTokenTest.java index 45e945197953..a7438f1e55cf 100644 --- a/guava-tests/test/com/google/common/reflect/TypeTokenTest.java +++ b/guava-tests/test/com/google/common/reflect/TypeTokenTest.java @@ -343,23 +343,19 @@ public void testAssertSubtypeTokenBeforeSupertypeToken_subtypeFirst() { } public void testAssertSubtypeTokenBeforeSupertypeToken_supertypeFirst() { - try { - assertSubtypeTokenBeforeSupertypeToken( - ImmutableList.of(TypeToken.of(CharSequence.class), TypeToken.of(String.class))); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + assertSubtypeTokenBeforeSupertypeToken( + ImmutableList.of(TypeToken.of(CharSequence.class), TypeToken.of(String.class)))); } public void testAssertSubtypeTokenBeforeSupertypeToken_duplicate() { - try { - assertSubtypeTokenBeforeSupertypeToken( - ImmutableList.of(TypeToken.of(String.class), TypeToken.of(String.class))); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> + assertSubtypeTokenBeforeSupertypeToken( + ImmutableList.of(TypeToken.of(String.class), TypeToken.of(String.class)))); } public void testAssertSubtypeBeforeSupertype_empty() { @@ -375,21 +371,15 @@ public void testAssertSubtypeBeforeSupertype_subtypeFirst() { } public void testAssertSubtypeBeforeSupertype_supertypeFirst() { - try { - assertSubtypeBeforeSupertype(ImmutableList.of(CharSequence.class, String.class)); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> assertSubtypeBeforeSupertype(ImmutableList.of(CharSequence.class, String.class))); } public void testAssertSubtypeBeforeSupertype_duplicate() { - try { - assertSubtypeBeforeSupertype(ImmutableList.of(String.class, String.class)); - } catch (AssertionError expected) { - return; - } - fail(); + assertThrows( + AssertionError.class, + () -> assertSubtypeBeforeSupertype(ImmutableList.of(String.class, String.class))); } public void testGetGenericSuperclass_noSuperclass() { @@ -1768,11 +1758,7 @@ public void testRejectTypeVariable_withOwnerType() { } private static void assertHasTypeVariable(Type type) { - try { - TypeToken.of(type).rejectTypeVariables(); - fail("Should contain TypeVariable"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> TypeToken.of(type).rejectTypeVariables()); } private static void assertNoTypeVariable(Type type) { diff --git a/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java b/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java index 6712b3cf8d29..9cbaa3ad7d4c 100644 --- a/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java +++ b/guava-tests/test/com/google/common/reflect/TypeVisitorTest.java @@ -17,6 +17,7 @@ package com.google.common.reflect; import static com.google.common.reflect.Types.subtypeOf; +import static org.junit.Assert.assertThrows; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; @@ -103,16 +104,8 @@ void visitTypeVariable(TypeVariable t) { private static void assertVisited(Type type) { TypeVisitor visitor = new BaseTypeVisitor(); - try { - visitor.visit(type); - fail("Type not visited"); - } catch (UnsupportedOperationException expected) { - } - try { - visitor.visit(new Type[] {type}); - fail("Type not visited"); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, () -> visitor.visit(type)); + assertThrows(UnsupportedOperationException.class, () -> visitor.visit(new Type[] {type})); } private static class BaseTypeVisitor extends TypeVisitor { diff --git a/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java b/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java index ba6bf8702ef4..d71496a2d571 100644 --- a/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/ClosingFutureFinishToValueAndCloserTest.java @@ -86,12 +86,8 @@ T getFinalValue(ClosingFuture closingFuture) throws ExecutionException { void assertFinallyFailsWithException(ClosingFuture closingFuture) { assertThatFutureFailsWithException(closingFuture.statusFuture()); ValueAndCloser valueAndCloser = finishToValueAndCloser(closingFuture); - try { - valueAndCloser.get(); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isEqualTo(exception); - } + ExecutionException expected = assertThrows(ExecutionException.class, valueAndCloser::get); + assertThat(expected).hasCauseThat().isEqualTo(exception); valueAndCloser.closeAsync(); } diff --git a/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java b/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java index b1fac0561880..14d2b3fb217a 100644 --- a/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/ForwardingObjectTesterTest.java @@ -16,6 +16,8 @@ package com.google.common.util.concurrent; +import static org.junit.Assert.assertThrows; + import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.collect.ForwardingObject; @@ -33,13 +35,13 @@ public class ForwardingObjectTesterTest extends TestCase { public void testFailsToForward() { - try { - ForwardingObjectTester.testForwardingObject(FailToForward.class); - } catch (AssertionError | UnsupportedOperationException expected) { - // UnsupportedOperationException is what we see on Android. - return; - } - fail("Should have thrown"); + Throwable expected = + assertThrows( + Throwable.class, + () -> ForwardingObjectTester.testForwardingObject(FailToForward.class)); + assertTrue( + "Expected AssertionError or UnsupportedOperationException", + expected instanceof AssertionError || expected instanceof UnsupportedOperationException); } @AndroidIncompatible // TODO(cpovirk): java.lang.IllegalAccessError: superclass not accessible diff --git a/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java b/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java index b7c749ad766e..8617c6ab0bef 100644 --- a/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java +++ b/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java @@ -23,7 +23,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import java.util.concurrent.ExecutionException; @@ -36,23 +36,13 @@ final class TestPlatform { static void verifyGetOnPendingFuture(Future future) { checkNotNull(future); - try { - pseudoTimedGetUninterruptibly(future, 10, MILLISECONDS); - fail(); - } catch (TimeoutException expected) { - } catch (ExecutionException e) { - throw new AssertionError(e); - } + assertThrows( + TimeoutException.class, () -> pseudoTimedGetUninterruptibly(future, 10, MILLISECONDS)); } static void verifyTimedGetOnPendingFuture(Future future) { - try { - getUninterruptibly(future, 0, SECONDS); - fail(); - } catch (TimeoutException expected) { - } catch (ExecutionException e) { - throw new AssertionError(e); - } + checkNotNull(future); + assertThrows(TimeoutException.class, () -> getUninterruptibly(future, 0, SECONDS)); } static void verifyThreadWasNotInterrupted() {