From ea57186acd66e9c9a21ab3e22548d720a97a337e Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 26 Jun 2026 21:40:51 -0700 Subject: [PATCH] Use Truth a little better, and use `assertThrows` in a place that I'd missed. RELNOTES=n/a PiperOrigin-RevId: 938918661 --- .../test/com/google/common/primitives/CharsTest.java | 4 +--- .../test/com/google/common/primitives/IntsTest.java | 4 +--- .../test/com/google/common/primitives/ShortsTest.java | 4 +--- .../com/google/common/primitives/SignedBytesTest.java | 11 +++-------- .../google/common/primitives/UnsignedBytesTest.java | 4 +--- .../test/com/google/common/primitives/CharsTest.java | 4 +--- .../test/com/google/common/primitives/IntsTest.java | 4 +--- .../test/com/google/common/primitives/ShortsTest.java | 4 +--- .../com/google/common/primitives/SignedBytesTest.java | 11 +++-------- .../google/common/primitives/UnsignedBytesTest.java | 4 +--- 10 files changed, 14 insertions(+), 40 deletions(-) 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 86955755e37f..72d0b9c343ed 100644 --- a/android/guava-tests/test/com/google/common/primitives/CharsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/CharsTest.java @@ -86,9 +86,7 @@ public void testSaturatedCast() { private void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } // We need to test that our method behaves like the JDK method. 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 08c32a612bd0..c8ee1f7badca 100644 --- a/android/guava-tests/test/com/google/common/primitives/IntsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/IntsTest.java @@ -91,9 +91,7 @@ public void testSaturatedCast() { private static void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } // We need to test that our method behaves like the JDK method. 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 f202d8520af4..e89975416e29 100644 --- a/android/guava-tests/test/com/google/common/primitives/ShortsTest.java +++ b/android/guava-tests/test/com/google/common/primitives/ShortsTest.java @@ -90,9 +90,7 @@ public void testSaturatedCast() { private static void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } // We need to test that our method behaves like the JDK method. diff --git a/android/guava-tests/test/com/google/common/primitives/SignedBytesTest.java b/android/guava-tests/test/com/google/common/primitives/SignedBytesTest.java index be9a76398907..0e5daaf664ff 100644 --- a/android/guava-tests/test/com/google/common/primitives/SignedBytesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/SignedBytesTest.java @@ -72,14 +72,9 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - SignedBytes.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, () -> SignedBytes.checkedCast(value)); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } public void testCompare() { 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 abf4b706c672..4fa386c6dd24 100644 --- a/android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java @@ -87,9 +87,7 @@ public void testSaturatedCast() { private static void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } public void testCompare() { diff --git a/guava-tests/test/com/google/common/primitives/CharsTest.java b/guava-tests/test/com/google/common/primitives/CharsTest.java index 86955755e37f..72d0b9c343ed 100644 --- a/guava-tests/test/com/google/common/primitives/CharsTest.java +++ b/guava-tests/test/com/google/common/primitives/CharsTest.java @@ -86,9 +86,7 @@ public void testSaturatedCast() { private void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } // We need to test that our method behaves like the JDK method. diff --git a/guava-tests/test/com/google/common/primitives/IntsTest.java b/guava-tests/test/com/google/common/primitives/IntsTest.java index 08c32a612bd0..c8ee1f7badca 100644 --- a/guava-tests/test/com/google/common/primitives/IntsTest.java +++ b/guava-tests/test/com/google/common/primitives/IntsTest.java @@ -91,9 +91,7 @@ public void testSaturatedCast() { private static void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } // We need to test that our method behaves like the JDK method. diff --git a/guava-tests/test/com/google/common/primitives/ShortsTest.java b/guava-tests/test/com/google/common/primitives/ShortsTest.java index f202d8520af4..e89975416e29 100644 --- a/guava-tests/test/com/google/common/primitives/ShortsTest.java +++ b/guava-tests/test/com/google/common/primitives/ShortsTest.java @@ -90,9 +90,7 @@ public void testSaturatedCast() { private static void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } // We need to test that our method behaves like the JDK method. diff --git a/guava-tests/test/com/google/common/primitives/SignedBytesTest.java b/guava-tests/test/com/google/common/primitives/SignedBytesTest.java index be9a76398907..0e5daaf664ff 100644 --- a/guava-tests/test/com/google/common/primitives/SignedBytesTest.java +++ b/guava-tests/test/com/google/common/primitives/SignedBytesTest.java @@ -72,14 +72,9 @@ public void testSaturatedCast() { } private static void assertCastFails(long value) { - try { - SignedBytes.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, () -> SignedBytes.checkedCast(value)); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } public void testCompare() { diff --git a/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java b/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java index b00f6aab714e..7876350cdd39 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java @@ -89,9 +89,7 @@ public void testSaturatedCast() { private static void assertCastFails(long value) { 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(); + assertThat(ex).hasMessageThat().contains(String.valueOf(value)); } public void testCompare() {