From 7a61ca9bffebc4de97b6cb1519c5f1b869f1f3fe Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 15 Nov 2024 10:28:31 -0800 Subject: [PATCH] Use `hasMessageThat` and `hasCauseThat`. I notice a few more `try`-`fail` tests that should use `assertThrows` but were missed by our automation for whatever reason.... PiperOrigin-RevId: 696930033 --- .../common/testing/EquivalenceTesterTest.java | 9 +++-- .../ForwardingWrapperTesterTest.java | 2 +- .../common/collect/ImmutableBiMapTest.java | 6 ++-- .../com/google/common/collect/MapsTest.java | 2 +- .../AbstractStandardDirectedNetworkTest.java | 8 ++--- .../com/google/common/graph/GraphsTest.java | 10 +++--- .../google/common/net/InetAddressesTest.java | 26 ++++++++------ .../AbstractAbstractFutureTest.java | 2 +- .../AbstractExecutionThreadServiceTest.java | 2 +- .../util/concurrent/AbstractFutureTest.java | 4 +-- .../util/concurrent/FakeTimeLimiterTest.java | 8 ++--- .../concurrent/FuturesGetUncheckedTest.java | 9 ++--- .../common/util/concurrent/FuturesTest.java | 36 +++++++++---------- .../util/concurrent/ServiceManagerTest.java | 5 +-- .../concurrent/SimpleTimeLimiterTest.java | 8 ++--- .../common/testing/EquivalenceTesterTest.java | 9 +++-- .../common/testing/NullPointerTesterTest.java | 10 +++--- .../ForwardingWrapperTesterTest.java | 2 +- .../common/collect/ImmutableBiMapTest.java | 6 ++-- .../com/google/common/collect/MapsTest.java | 2 +- .../common/collect/MoreCollectorsTest.java | 8 ++--- .../AbstractStandardDirectedNetworkTest.java | 8 ++--- .../com/google/common/graph/GraphsTest.java | 10 +++--- .../google/common/net/InetAddressesTest.java | 26 ++++++++------ .../AbstractAbstractFutureTest.java | 2 +- .../AbstractExecutionThreadServiceTest.java | 2 +- .../util/concurrent/AbstractFutureTest.java | 4 +-- .../util/concurrent/FakeTimeLimiterTest.java | 8 ++--- .../concurrent/FuturesGetUncheckedTest.java | 9 ++--- .../common/util/concurrent/FuturesTest.java | 36 +++++++++---------- .../util/concurrent/ServiceManagerTest.java | 5 +-- .../concurrent/SimpleTimeLimiterTest.java | 8 ++--- 32 files changed, 157 insertions(+), 135 deletions(-) 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 00e9e5da9522..7e9649090289 100644 --- a/android/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java +++ b/android/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java @@ -101,7 +101,8 @@ public void testTest_symmetric() { try { tester.addEquivalenceGroup(group1Item1, group1Item2).test(); } catch (AssertionFailedError expected) { - assertThat(expected.getMessage()) + 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]"); @@ -131,7 +132,8 @@ public void testTest_transitive() { try { tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test(); } catch (AssertionFailedError expected) { - assertThat(expected.getMessage()) + 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]"); @@ -155,7 +157,8 @@ public void testTest_inequivalence() { try { tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test(); } catch (AssertionFailedError expected) { - assertThat(expected.getMessage()) + 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]"); 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 17655f6ef844..6365c5be1e9b 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 @@ -287,7 +287,7 @@ private void assertFailure( tester.testForwarding(interfaceType, wrapperFunction); } catch (AssertionFailedError expected) { for (String message : expectedMessages) { - assertThat(expected.getMessage()).contains(message); + assertThat(expected).hasMessageThat().contains(message); } return; } diff --git a/android/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java b/android/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java index 60ad4d437953..34d186439883 100644 --- a/android/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java +++ b/android/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java @@ -244,7 +244,7 @@ public void testPuttingTheSameKeyTwiceThrowsOnBuild() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> builder.build()); - assertThat(expected.getMessage()).contains("one"); + assertThat(expected).hasMessageThat().contains("one"); } public void testOf() { @@ -454,7 +454,7 @@ public void testOfNullValue() { public void testOfWithDuplicateKey() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> ImmutableBiMap.of("one", 1, "one", 1)); - assertThat(expected.getMessage()).contains("one"); + assertThat(expected).hasMessageThat().contains("one"); } public void testOfEntries() { @@ -542,7 +542,7 @@ public void testDuplicateValues() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> ImmutableBiMap.copyOf(map)); - assertThat(expected.getMessage()).containsMatch("1|2"); + assertThat(expected).hasMessageThat().containsMatch("1|2"); } // TODO(b/172823566): Use mainline testToImmutableBiMap once CollectorTester is usable to java7. diff --git a/android/guava-tests/test/com/google/common/collect/MapsTest.java b/android/guava-tests/test/com/google/common/collect/MapsTest.java index 8a2103c01b90..ac501a51e096 100644 --- a/android/guava-tests/test/com/google/common/collect/MapsTest.java +++ b/android/guava-tests/test/com/google/common/collect/MapsTest.java @@ -960,7 +960,7 @@ public void testUniqueIndexDuplicates() { assertThrows( IllegalArgumentException.class, () -> Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1))); - assertThat(expected.getMessage()).contains("Multimaps.index"); + assertThat(expected).hasMessageThat().contains("Multimaps.index"); } /** Null values are not allowed. */ diff --git a/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java b/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java index 40f9a3a6948e..19a28cd76d7c 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java @@ -587,16 +587,16 @@ public void addEdge_existingEdgeBetweenDifferentNodes_selfLoops() { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N2, E11)); - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); e = assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N2, N2, E11)); - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); addEdge(N1, N2, E12); e = assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E12)); - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); } @Test @@ -610,7 +610,7 @@ public void addEdge_parallelSelfLoopEdge_notAllowed() { assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/GraphsTest.java b/android/guava-tests/test/com/google/common/graph/GraphsTest.java index 36ee07cb1f84..859066695ffa 100644 --- a/android/guava-tests/test/com/google/common/graph/GraphsTest.java +++ b/android/guava-tests/test/com/google/common/graph/GraphsTest.java @@ -467,7 +467,7 @@ public void createDirected() { // By default, parallel edges are not allowed. IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N2, E12_A)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); // By default, self-loop edges are not allowed. e = assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N1, E11)); @@ -486,9 +486,9 @@ public void createUndirected() { // By default, parallel edges are not allowed. IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N2, E12_A)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N2, N1, E21)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); // By default, self-loop edges are not allowed. e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N1, E11)); @@ -539,7 +539,7 @@ public void builder_expectedNodeCount_negative() { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedNodeCount(-1)); - assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); + assertThat(e).hasMessageThat().contains(ERROR_NEGATIVE_COUNT); } @Test @@ -565,7 +565,7 @@ public void builder_expectedEdgeCount_negative() { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedEdgeCount(-1)); - assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); + assertThat(e).hasMessageThat().contains(ERROR_NEGATIVE_COUNT); } private static void checkTransitiveClosure(Graph originalGraph, Graph expectedClosure) { diff --git a/android/guava-tests/test/com/google/common/net/InetAddressesTest.java b/android/guava-tests/test/com/google/common/net/InetAddressesTest.java index c31c6e23fb40..5bf1ee861a16 100644 --- a/android/guava-tests/test/com/google/common/net/InetAddressesTest.java +++ b/android/guava-tests/test/com/google/common/net/InetAddressesTest.java @@ -812,7 +812,9 @@ public void testFromIpv4BigIntegerThrowsLessThanZero() { assertThrows( IllegalArgumentException.class, () -> InetAddresses.fromIPv4BigInteger(BigInteger.valueOf(-1L))); - assertEquals("BigInteger must be greater than or equal to 0", expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo("BigInteger must be greater than or equal to 0"); } public void testFromIpv6BigIntegerThrowsLessThanZero() { @@ -820,7 +822,9 @@ public void testFromIpv6BigIntegerThrowsLessThanZero() { assertThrows( IllegalArgumentException.class, () -> InetAddresses.fromIPv6BigInteger(BigInteger.valueOf(-1L))); - assertEquals("BigInteger must be greater than or equal to 0", expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo("BigInteger must be greater than or equal to 0"); } public void testFromIpv4BigIntegerValid() { @@ -851,10 +855,11 @@ public void testFromIpv4BigIntegerInputTooLarge() { IllegalArgumentException.class, () -> InetAddresses.fromIPv4BigInteger(BigInteger.ONE.shiftLeft(32).add(BigInteger.ONE))); - assertEquals( - "BigInteger cannot be converted to InetAddress because it has more than 4 bytes:" - + " 4294967297", - expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "BigInteger cannot be converted to InetAddress because it has more than 4 bytes:" + + " 4294967297"); } public void testFromIpv6BigIntegerInputTooLarge() { @@ -864,10 +869,11 @@ public void testFromIpv6BigIntegerInputTooLarge() { () -> InetAddresses.fromIPv6BigInteger( BigInteger.ONE.shiftLeft(128).add(BigInteger.ONE))); - assertEquals( - "BigInteger cannot be converted to InetAddress because it has more than 16 bytes:" - + " 340282366920938463463374607431768211457", - expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "BigInteger cannot be converted to InetAddress because it has more than 16 bytes:" + + " 340282366920938463463374607431768211457"); } // see https://github.com/google/guava/issues/2587 diff --git a/android/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java b/android/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java index 6350584cbeed..c1297abc2fae 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java @@ -447,7 +447,7 @@ private static void assertFailed(AbstractFuture future, Throwable expec getDone(future); fail(); } catch (ExecutionException e) { - assertThat(e.getCause()).isSameInstanceAs(expectedException); + assertThat(e).hasCauseThat().isSameInstanceAs(expectedException); } try { diff --git a/android/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java b/android/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java index e435d7d49d1b..cd7fc344ae05 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java @@ -298,7 +298,7 @@ public void testServiceTimeoutOnStartUp() throws Exception { TimeoutException e = assertThrows( TimeoutException.class, () -> service.startAsync().awaitRunning(1, MILLISECONDS)); - assertThat(e.getMessage()).contains(Service.State.STARTING.toString()); + assertThat(e).hasMessageThat().contains(Service.State.STARTING.toString()); } private class TimeoutOnStartUp extends AbstractExecutionThreadService { 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 e251c0fdf759..f66383e198a8 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 @@ -262,8 +262,8 @@ public String pendingToString() { .matches( "[^\\[]+\\[status=PENDING, info=\\[cause=\\[Because this test isn't done\\]\\]\\]"); TimeoutException e = assertThrows(TimeoutException.class, () -> testFuture.get(1, NANOSECONDS)); - assertThat(e.getMessage()).contains("1 nanoseconds"); - assertThat(e.getMessage()).contains("Because this test isn't done"); + assertThat(e).hasMessageThat().contains("1 nanoseconds"); + assertThat(e).hasMessageThat().contains("Because this test isn't done"); } public void testToString_completesDuringToString() throws Exception { diff --git a/android/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java b/android/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java index 6298df7904fa..78f4374d7b03 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java @@ -55,7 +55,7 @@ public void testCallWithTimeout_wrapsCheckedException() throws Exception { assertThrows( ExecutionException.class, () -> timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public void testCallWithTimeout_wrapsUncheckedException() throws Exception { @@ -64,7 +64,7 @@ public void testCallWithTimeout_wrapsUncheckedException() throws Exception { assertThrows( UncheckedExecutionException.class, () -> timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public void testCallUninterruptiblyWithTimeout_propagatesReturnValue() throws Exception { @@ -85,7 +85,7 @@ public void testRunWithTimeout_wrapsUncheckedException() throws Exception { assertThrows( UncheckedExecutionException.class, () -> timeLimiter.runWithTimeout(runnableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws Exception { @@ -96,7 +96,7 @@ public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws E () -> timeLimiter.runUninterruptiblyWithTimeout( runnableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public static Callable callableThrowing(final Exception exception) { diff --git a/android/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java b/android/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java index 5916777683a8..3d2e1673986b 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java @@ -14,6 +14,7 @@ package com.google.common.util.concurrent; +import static com.google.common.truth.Truth.assertThat; import static com.google.common.util.concurrent.Futures.getUnchecked; import static com.google.common.util.concurrent.Futures.immediateFuture; import static com.google.common.util.concurrent.FuturesGetCheckedInputs.CHECKED_EXCEPTION; @@ -65,7 +66,7 @@ public void testGetUnchecked_executionExceptionChecked() { UncheckedExecutionException expected = assertThrows( UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_CHECKED_EXCEPTION)); - assertEquals(CHECKED_EXCEPTION, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION); } public void testGetUnchecked_executionExceptionUnchecked() { @@ -73,20 +74,20 @@ public void testGetUnchecked_executionExceptionUnchecked() { assertThrows( UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_UNCHECKED_EXCEPTION)); - assertEquals(UNCHECKED_EXCEPTION, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(UNCHECKED_EXCEPTION); } public void testGetUnchecked_executionExceptionError() { ExecutionError expected = assertThrows(ExecutionError.class, () -> getUnchecked(FAILED_FUTURE_ERROR)); - assertEquals(ERROR, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(ERROR); } public void testGetUnchecked_executionExceptionOtherThrowable() { UncheckedExecutionException expected = assertThrows( UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_OTHER_THROWABLE)); - assertEquals(OTHER_THROWABLE, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(OTHER_THROWABLE); } public void testGetUnchecked_runtimeException() { diff --git a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index 5ed0bbfb802a..ade301520edc 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -195,8 +195,8 @@ public void testImmediateCancelledFutureStack() throws Exception { assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class))); assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class))); - // See AbstractFutureCancellationCauseTest for how to set causes. - assertThat(expected.getCause()).isNull(); + // See AbstractFutureCancellationCauseTest for how to set causes. + assertThat(expected).hasCauseThat().isNull(); } @J2ktIncompatible @@ -330,7 +330,7 @@ public void testTransform_getThrowsRuntimeException() throws Exception { ListenableFuture output = transform(input, identity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeUncheckedException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeUncheckedException.class); } public void testTransform_getThrowsError() throws Exception { @@ -338,7 +338,7 @@ public void testTransform_getThrowsError() throws Exception { ListenableFuture output = transform(input, identity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeError.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeError.class); } public void testTransform_listenerThrowsError() throws Exception { @@ -546,7 +546,7 @@ public void testTransformAsync_getThrowsRuntimeException() throws Exception { ListenableFuture output = transformAsync(input, asyncIdentity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeUncheckedException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeUncheckedException.class); } public void testTransformAsync_getThrowsError() throws Exception { @@ -554,7 +554,7 @@ public void testTransformAsync_getThrowsError() throws Exception { ListenableFuture output = transformAsync(input, asyncIdentity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeError.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeError.class); } public void testTransformAsync_listenerThrowsError() throws Exception { @@ -579,7 +579,7 @@ public void testTransform_rejectionPropagatesToOutput() throws Exception { input.set(new Foo()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } public void testTransformAsync_rejectionPropagatesToOutput() throws Exception { @@ -589,7 +589,7 @@ public void testTransformAsync_rejectionPropagatesToOutput() throws Exception { input.set(new Foo()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } /** Tests that the function is invoked only once, even if it throws an exception. */ @@ -1312,7 +1312,7 @@ public void testCatching_customTypeNoMatch() throws Exception { catching(originalFuture, IOException.class, fallback, directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(faultTolerantFuture)); - assertThat(expected.getCause()).isInstanceOf(RuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RuntimeException.class); } @J2ktIncompatible @@ -1441,7 +1441,7 @@ public void testCatchingAsync_customTypeNoMatch() throws Exception { catchingAsync(originalFuture, IOException.class, fallback, directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(faultTolerantFuture)); - assertThat(expected.getCause()).isInstanceOf(RuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RuntimeException.class); } @J2ktIncompatible @@ -1552,7 +1552,7 @@ public void testCatching_rejectionPropagatesToOutput() throws Exception { input.setException(new Exception()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } public void testCatchingAsync_rejectionPropagatesToOutput() throws Exception { @@ -1566,7 +1566,7 @@ public void testCatchingAsync_rejectionPropagatesToOutput() throws Exception { input.setException(new Exception()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } private Function functionReturningOne() { @@ -2239,7 +2239,7 @@ public void testAllAsList_logging_exception() throws Exception { assertThrows( ExecutionException.class, () -> getDone(allAsList(immediateFailedFuture(new MyException())))); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } @@ -2250,7 +2250,7 @@ public void testAllAsList_logging_error() throws Exception { assertThrows( ExecutionException.class, () -> getDone(allAsList(immediateFailedFuture(new SomeError())))); - assertThat(expected.getCause()).isInstanceOf(SomeError.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeError.class); List logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(SomeError.class); @@ -2266,7 +2266,7 @@ public void testAllAsList_logging_multipleExceptions_alreadyDone() throws Except allAsList( immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException())))); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); List logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // the second failure is logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyException.class); @@ -2301,7 +2301,7 @@ public void testAllAsList_logging_same_exception() throws Exception { allAsList( immediateFailedFuture(sameInstance), immediateFailedFuture(sameInstance))); }); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } @@ -2328,7 +2328,7 @@ public void run() { firstFuture.setException(sameInstance); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(bulkFuture)); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertThat(aggregateFutureLogHandler.getStoredLogRecords()).isEmpty(); } @@ -2377,7 +2377,7 @@ public void testAllAsList_logging_same_cause() throws Exception { getDone( allAsList(immediateFailedFuture(exception1), immediateFailedFuture(exception3))); }); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } diff --git a/android/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java b/android/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java index 0fe1d849a7e9..12052571452a 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java @@ -448,7 +448,8 @@ public void testStartupFailureOutput() { assertThat(manager.servicesByState().get(State.FAILED)).hasSize(2); IllegalStateException e = assertThrows(IllegalStateException.class, () -> manager.awaitHealthy()); - assertThat(e.getMessage()) + assertThat(e) + .hasMessageThat() .contains( "Expected to be healthy after starting. The following services are not " + "running:"); @@ -614,7 +615,7 @@ public final Throwable failureCause() { assertThrows( IllegalArgumentException.class, () -> new ServiceManager(Arrays.asList(service1, service2))); - assertThat(expected.getMessage()).contains("started transitioning asynchronously"); + assertThat(expected).hasMessageThat().contains("started transitioning asynchronously"); } /** diff --git a/android/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java b/android/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java index f66b9ed47aa7..314b754aa833 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java @@ -158,7 +158,7 @@ public void testCallWithTimeout_badCallableWithEnoughTime() throws Exception { assertThrows( ExecutionException.class, () -> service.callWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleException.class); } public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception { @@ -181,7 +181,7 @@ public void testCallUninterruptiblyWithTimeout_badCallableWithEnoughTime() throw assertThrows( ExecutionException.class, () -> service.callUninterruptiblyWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleException.class); } public void testRunWithTimeout_goodRunnableWithEnoughTime() throws Exception { @@ -203,7 +203,7 @@ public void testRunWithTimeout_badRunnableWithEnoughTime() throws Exception { assertThrows( UncheckedExecutionException.class, () -> service.runWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleRuntimeException.class); } public void testRunUninterruptiblyWithTimeout_goodRunnableWithEnoughTime() throws Exception { @@ -225,7 +225,7 @@ public void testRunUninterruptiblyWithTimeout_badRunnableWithEnoughTime() throws assertThrows( UncheckedExecutionException.class, () -> service.runUninterruptiblyWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleRuntimeException.class); } private interface Sample { diff --git a/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java b/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java index 00e9e5da9522..7e9649090289 100644 --- a/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/EquivalenceTesterTest.java @@ -101,7 +101,8 @@ public void testTest_symmetric() { try { tester.addEquivalenceGroup(group1Item1, group1Item2).test(); } catch (AssertionFailedError expected) { - assertThat(expected.getMessage()) + 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]"); @@ -131,7 +132,8 @@ public void testTest_transitive() { try { tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test(); } catch (AssertionFailedError expected) { - assertThat(expected.getMessage()) + 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]"); @@ -155,7 +157,8 @@ public void testTest_inequivalence() { try { tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test(); } catch (AssertionFailedError expected) { - assertThat(expected.getMessage()) + 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]"); diff --git a/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java b/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java index a2477677313d..3210b7aab67b 100644 --- a/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java @@ -321,8 +321,8 @@ public void testMessageOtherException() throws Exception { try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); } catch (AssertionError expected) { - assertThat(expected.getMessage()).contains("index 0"); - assertThat(expected.getMessage()).contains("[null]"); + assertThat(expected).hasMessageThat().contains("index 0"); + assertThat(expected).hasMessageThat().contains("[null]"); foundProblem = true; } assertTrue("Should report error when different exception is thrown", foundProblem); @@ -334,8 +334,8 @@ public void testMessageNoException() throws Exception { try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); } catch (AssertionError expected) { - assertThat(expected.getMessage()).contains("index 0"); - assertThat(expected.getMessage()).contains("[null]"); + assertThat(expected).hasMessageThat().contains("index 0"); + assertThat(expected).hasMessageThat().contains("[null]"); foundProblem = true; } assertTrue("Should report error when no exception is thrown", foundProblem); @@ -1435,7 +1435,7 @@ public void testNonStaticInnerClass() { assertThrows( IllegalArgumentException.class, () -> new NullPointerTester().testAllPublicConstructors(Inner.class)); - assertThat(expected.getMessage()).contains("inner class"); + assertThat(expected).hasMessageThat().contains("inner class"); } private static String rootLocaleFormat(String format, Object... args) { 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 17655f6ef844..6365c5be1e9b 100644 --- a/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java +++ b/guava-testlib/test/com/google/common/testing/anotherpackage/ForwardingWrapperTesterTest.java @@ -287,7 +287,7 @@ private void assertFailure( tester.testForwarding(interfaceType, wrapperFunction); } catch (AssertionFailedError expected) { for (String message : expectedMessages) { - assertThat(expected.getMessage()).contains(message); + assertThat(expected).hasMessageThat().contains(message); } return; } diff --git a/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java b/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java index 0b36732a9c4a..72c935c8f3d0 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableBiMapTest.java @@ -271,7 +271,7 @@ public void testPuttingTheSameKeyTwiceThrowsOnBuild() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> builder.build()); - assertThat(expected.getMessage()).contains("one"); + assertThat(expected).hasMessageThat().contains("one"); } public void testOf() { @@ -481,7 +481,7 @@ public void testOfNullValue() { public void testOfWithDuplicateKey() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> ImmutableBiMap.of("one", 1, "one", 1)); - assertThat(expected.getMessage()).contains("one"); + assertThat(expected).hasMessageThat().contains("one"); } public void testOfEntries() { @@ -569,7 +569,7 @@ public void testDuplicateValues() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> ImmutableBiMap.copyOf(map)); - assertThat(expected.getMessage()).containsMatch("1|2"); + assertThat(expected).hasMessageThat().containsMatch("1|2"); } public void testToImmutableBiMap() { diff --git a/guava-tests/test/com/google/common/collect/MapsTest.java b/guava-tests/test/com/google/common/collect/MapsTest.java index 8fc865bfe24c..67b8c934f6dc 100644 --- a/guava-tests/test/com/google/common/collect/MapsTest.java +++ b/guava-tests/test/com/google/common/collect/MapsTest.java @@ -960,7 +960,7 @@ public void testUniqueIndexDuplicates() { assertThrows( IllegalArgumentException.class, () -> Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1))); - assertThat(expected.getMessage()).contains("Multimaps.index"); + assertThat(expected).hasMessageThat().contains("Multimaps.index"); } /** Null values are not allowed. */ diff --git a/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java b/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java index 48434f5fb267..7b1e66b23996 100644 --- a/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java +++ b/guava-tests/test/com/google/common/collect/MoreCollectorsTest.java @@ -51,7 +51,7 @@ public void testToOptionalNull() { public void testToOptionalMultiple() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> Stream.of(1, 2).collect(toOptional())); - assertThat(expected.getMessage()).contains("1, 2"); + assertThat(expected).hasMessageThat().contains("1, 2"); } public void testToOptionalMultipleWithNull() { @@ -63,7 +63,7 @@ public void testToOptionalMany() { assertThrows( IllegalArgumentException.class, () -> Stream.of(1, 2, 3, 4, 5, 6).collect(toOptional())); - assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); + assertThat(expected).hasMessageThat().contains("1, 2, 3, 4, 5, ..."); } public void testOnlyElement() { @@ -81,7 +81,7 @@ public void testOnlyElementNull() { public void testOnlyElementMultiple() { IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> Stream.of(1, 2).collect(onlyElement())); - assertThat(expected.getMessage()).contains("1, 2"); + assertThat(expected).hasMessageThat().contains("1, 2"); } public void testOnlyElementMany() { @@ -89,6 +89,6 @@ public void testOnlyElementMany() { assertThrows( IllegalArgumentException.class, () -> Stream.of(1, 2, 3, 4, 5, 6).collect(onlyElement())); - assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); + assertThat(expected).hasMessageThat().contains("1, 2, 3, 4, 5, ..."); } } diff --git a/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java b/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java index 107aa3e57b3a..f33c34d9affe 100644 --- a/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java +++ b/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java @@ -600,16 +600,16 @@ public void addEdge_existingEdgeBetweenDifferentNodes_selfLoops() { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N2, E11)); - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); e = assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N2, N2, E11)); - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); addEdge(N1, N2, E12); e = assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E12)); - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); } @Test @@ -623,7 +623,7 @@ public void addEdge_parallelSelfLoopEdge_notAllowed() { assertThrows( IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); } @Test diff --git a/guava-tests/test/com/google/common/graph/GraphsTest.java b/guava-tests/test/com/google/common/graph/GraphsTest.java index 53a14d548d8a..ff59ff7a5d71 100644 --- a/guava-tests/test/com/google/common/graph/GraphsTest.java +++ b/guava-tests/test/com/google/common/graph/GraphsTest.java @@ -468,7 +468,7 @@ public void createDirected() { // By default, parallel edges are not allowed. IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N2, E12_A)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); // By default, self-loop edges are not allowed. e = assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N1, E11)); @@ -487,9 +487,9 @@ public void createUndirected() { // By default, parallel edges are not allowed. IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N2, E12_A)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N2, N1, E21)); - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); // By default, self-loop edges are not allowed. e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N1, E11)); @@ -540,7 +540,7 @@ public void builder_expectedNodeCount_negative() { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedNodeCount(-1)); - assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); + assertThat(e).hasMessageThat().contains(ERROR_NEGATIVE_COUNT); } @Test @@ -566,7 +566,7 @@ public void builder_expectedEdgeCount_negative() { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedEdgeCount(-1)); - assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); + assertThat(e).hasMessageThat().contains(ERROR_NEGATIVE_COUNT); } private static void checkTransitiveClosure(Graph originalGraph, Graph expectedClosure) { diff --git a/guava-tests/test/com/google/common/net/InetAddressesTest.java b/guava-tests/test/com/google/common/net/InetAddressesTest.java index c31c6e23fb40..5bf1ee861a16 100644 --- a/guava-tests/test/com/google/common/net/InetAddressesTest.java +++ b/guava-tests/test/com/google/common/net/InetAddressesTest.java @@ -812,7 +812,9 @@ public void testFromIpv4BigIntegerThrowsLessThanZero() { assertThrows( IllegalArgumentException.class, () -> InetAddresses.fromIPv4BigInteger(BigInteger.valueOf(-1L))); - assertEquals("BigInteger must be greater than or equal to 0", expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo("BigInteger must be greater than or equal to 0"); } public void testFromIpv6BigIntegerThrowsLessThanZero() { @@ -820,7 +822,9 @@ public void testFromIpv6BigIntegerThrowsLessThanZero() { assertThrows( IllegalArgumentException.class, () -> InetAddresses.fromIPv6BigInteger(BigInteger.valueOf(-1L))); - assertEquals("BigInteger must be greater than or equal to 0", expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo("BigInteger must be greater than or equal to 0"); } public void testFromIpv4BigIntegerValid() { @@ -851,10 +855,11 @@ public void testFromIpv4BigIntegerInputTooLarge() { IllegalArgumentException.class, () -> InetAddresses.fromIPv4BigInteger(BigInteger.ONE.shiftLeft(32).add(BigInteger.ONE))); - assertEquals( - "BigInteger cannot be converted to InetAddress because it has more than 4 bytes:" - + " 4294967297", - expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "BigInteger cannot be converted to InetAddress because it has more than 4 bytes:" + + " 4294967297"); } public void testFromIpv6BigIntegerInputTooLarge() { @@ -864,10 +869,11 @@ public void testFromIpv6BigIntegerInputTooLarge() { () -> InetAddresses.fromIPv6BigInteger( BigInteger.ONE.shiftLeft(128).add(BigInteger.ONE))); - assertEquals( - "BigInteger cannot be converted to InetAddress because it has more than 16 bytes:" - + " 340282366920938463463374607431768211457", - expected.getMessage()); + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "BigInteger cannot be converted to InetAddress because it has more than 16 bytes:" + + " 340282366920938463463374607431768211457"); } // see https://github.com/google/guava/issues/2587 diff --git a/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java b/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java index 6350584cbeed..c1297abc2fae 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/AbstractAbstractFutureTest.java @@ -447,7 +447,7 @@ private static void assertFailed(AbstractFuture future, Throwable expec getDone(future); fail(); } catch (ExecutionException e) { - assertThat(e.getCause()).isSameInstanceAs(expectedException); + assertThat(e).hasCauseThat().isSameInstanceAs(expectedException); } try { diff --git a/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java b/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java index e435d7d49d1b..cd7fc344ae05 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/AbstractExecutionThreadServiceTest.java @@ -298,7 +298,7 @@ public void testServiceTimeoutOnStartUp() throws Exception { TimeoutException e = assertThrows( TimeoutException.class, () -> service.startAsync().awaitRunning(1, MILLISECONDS)); - assertThat(e.getMessage()).contains(Service.State.STARTING.toString()); + assertThat(e).hasMessageThat().contains(Service.State.STARTING.toString()); } private class TimeoutOnStartUp extends AbstractExecutionThreadService { 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 e251c0fdf759..f66383e198a8 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java @@ -262,8 +262,8 @@ public String pendingToString() { .matches( "[^\\[]+\\[status=PENDING, info=\\[cause=\\[Because this test isn't done\\]\\]\\]"); TimeoutException e = assertThrows(TimeoutException.class, () -> testFuture.get(1, NANOSECONDS)); - assertThat(e.getMessage()).contains("1 nanoseconds"); - assertThat(e.getMessage()).contains("Because this test isn't done"); + assertThat(e).hasMessageThat().contains("1 nanoseconds"); + assertThat(e).hasMessageThat().contains("Because this test isn't done"); } public void testToString_completesDuringToString() throws Exception { diff --git a/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java b/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java index 6298df7904fa..78f4374d7b03 100644 --- a/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/FakeTimeLimiterTest.java @@ -55,7 +55,7 @@ public void testCallWithTimeout_wrapsCheckedException() throws Exception { assertThrows( ExecutionException.class, () -> timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public void testCallWithTimeout_wrapsUncheckedException() throws Exception { @@ -64,7 +64,7 @@ public void testCallWithTimeout_wrapsUncheckedException() throws Exception { assertThrows( UncheckedExecutionException.class, () -> timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public void testCallUninterruptiblyWithTimeout_propagatesReturnValue() throws Exception { @@ -85,7 +85,7 @@ public void testRunWithTimeout_wrapsUncheckedException() throws Exception { assertThrows( UncheckedExecutionException.class, () -> timeLimiter.runWithTimeout(runnableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws Exception { @@ -96,7 +96,7 @@ public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws E () -> timeLimiter.runUninterruptiblyWithTimeout( runnableThrowing(exception), DELAY_MS, MILLISECONDS)); - assertThat(e.getCause()).isEqualTo(exception); + assertThat(e).hasCauseThat().isEqualTo(exception); } public static Callable callableThrowing(final Exception exception) { diff --git a/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java b/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java index 5916777683a8..3d2e1673986b 100644 --- a/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/FuturesGetUncheckedTest.java @@ -14,6 +14,7 @@ package com.google.common.util.concurrent; +import static com.google.common.truth.Truth.assertThat; import static com.google.common.util.concurrent.Futures.getUnchecked; import static com.google.common.util.concurrent.Futures.immediateFuture; import static com.google.common.util.concurrent.FuturesGetCheckedInputs.CHECKED_EXCEPTION; @@ -65,7 +66,7 @@ public void testGetUnchecked_executionExceptionChecked() { UncheckedExecutionException expected = assertThrows( UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_CHECKED_EXCEPTION)); - assertEquals(CHECKED_EXCEPTION, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION); } public void testGetUnchecked_executionExceptionUnchecked() { @@ -73,20 +74,20 @@ public void testGetUnchecked_executionExceptionUnchecked() { assertThrows( UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_UNCHECKED_EXCEPTION)); - assertEquals(UNCHECKED_EXCEPTION, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(UNCHECKED_EXCEPTION); } public void testGetUnchecked_executionExceptionError() { ExecutionError expected = assertThrows(ExecutionError.class, () -> getUnchecked(FAILED_FUTURE_ERROR)); - assertEquals(ERROR, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(ERROR); } public void testGetUnchecked_executionExceptionOtherThrowable() { UncheckedExecutionException expected = assertThrows( UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_OTHER_THROWABLE)); - assertEquals(OTHER_THROWABLE, expected.getCause()); + assertThat(expected).hasCauseThat().isEqualTo(OTHER_THROWABLE); } public void testGetUnchecked_runtimeException() { diff --git a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index 5ed0bbfb802a..ade301520edc 100644 --- a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -195,8 +195,8 @@ public void testImmediateCancelledFutureStack() throws Exception { assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class))); assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class))); - // See AbstractFutureCancellationCauseTest for how to set causes. - assertThat(expected.getCause()).isNull(); + // See AbstractFutureCancellationCauseTest for how to set causes. + assertThat(expected).hasCauseThat().isNull(); } @J2ktIncompatible @@ -330,7 +330,7 @@ public void testTransform_getThrowsRuntimeException() throws Exception { ListenableFuture output = transform(input, identity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeUncheckedException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeUncheckedException.class); } public void testTransform_getThrowsError() throws Exception { @@ -338,7 +338,7 @@ public void testTransform_getThrowsError() throws Exception { ListenableFuture output = transform(input, identity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeError.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeError.class); } public void testTransform_listenerThrowsError() throws Exception { @@ -546,7 +546,7 @@ public void testTransformAsync_getThrowsRuntimeException() throws Exception { ListenableFuture output = transformAsync(input, asyncIdentity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeUncheckedException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeUncheckedException.class); } public void testTransformAsync_getThrowsError() throws Exception { @@ -554,7 +554,7 @@ public void testTransformAsync_getThrowsError() throws Exception { ListenableFuture output = transformAsync(input, asyncIdentity(), directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(output)); - assertThat(expected.getCause()).isInstanceOf(SomeError.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeError.class); } public void testTransformAsync_listenerThrowsError() throws Exception { @@ -579,7 +579,7 @@ public void testTransform_rejectionPropagatesToOutput() throws Exception { input.set(new Foo()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } public void testTransformAsync_rejectionPropagatesToOutput() throws Exception { @@ -589,7 +589,7 @@ public void testTransformAsync_rejectionPropagatesToOutput() throws Exception { input.set(new Foo()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } /** Tests that the function is invoked only once, even if it throws an exception. */ @@ -1312,7 +1312,7 @@ public void testCatching_customTypeNoMatch() throws Exception { catching(originalFuture, IOException.class, fallback, directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(faultTolerantFuture)); - assertThat(expected.getCause()).isInstanceOf(RuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RuntimeException.class); } @J2ktIncompatible @@ -1441,7 +1441,7 @@ public void testCatchingAsync_customTypeNoMatch() throws Exception { catchingAsync(originalFuture, IOException.class, fallback, directExecutor()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(faultTolerantFuture)); - assertThat(expected.getCause()).isInstanceOf(RuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RuntimeException.class); } @J2ktIncompatible @@ -1552,7 +1552,7 @@ public void testCatching_rejectionPropagatesToOutput() throws Exception { input.setException(new Exception()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } public void testCatchingAsync_rejectionPropagatesToOutput() throws Exception { @@ -1566,7 +1566,7 @@ public void testCatchingAsync_rejectionPropagatesToOutput() throws Exception { input.setException(new Exception()); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(transformed)); - assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); + assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class); } private Function functionReturningOne() { @@ -2239,7 +2239,7 @@ public void testAllAsList_logging_exception() throws Exception { assertThrows( ExecutionException.class, () -> getDone(allAsList(immediateFailedFuture(new MyException())))); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } @@ -2250,7 +2250,7 @@ public void testAllAsList_logging_error() throws Exception { assertThrows( ExecutionException.class, () -> getDone(allAsList(immediateFailedFuture(new SomeError())))); - assertThat(expected.getCause()).isInstanceOf(SomeError.class); + assertThat(expected).hasCauseThat().isInstanceOf(SomeError.class); List logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(SomeError.class); @@ -2266,7 +2266,7 @@ public void testAllAsList_logging_multipleExceptions_alreadyDone() throws Except allAsList( immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException())))); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); List logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // the second failure is logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyException.class); @@ -2301,7 +2301,7 @@ public void testAllAsList_logging_same_exception() throws Exception { allAsList( immediateFailedFuture(sameInstance), immediateFailedFuture(sameInstance))); }); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } @@ -2328,7 +2328,7 @@ public void run() { firstFuture.setException(sameInstance); ExecutionException expected = assertThrows(ExecutionException.class, () -> getDone(bulkFuture)); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertThat(aggregateFutureLogHandler.getStoredLogRecords()).isEmpty(); } @@ -2377,7 +2377,7 @@ public void testAllAsList_logging_same_cause() throws Exception { getDone( allAsList(immediateFailedFuture(exception1), immediateFailedFuture(exception3))); }); - assertThat(expected.getCause()).isInstanceOf(MyException.class); + assertThat(expected).hasCauseThat().isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } diff --git a/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java b/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java index 0fe1d849a7e9..12052571452a 100644 --- a/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java @@ -448,7 +448,8 @@ public void testStartupFailureOutput() { assertThat(manager.servicesByState().get(State.FAILED)).hasSize(2); IllegalStateException e = assertThrows(IllegalStateException.class, () -> manager.awaitHealthy()); - assertThat(e.getMessage()) + assertThat(e) + .hasMessageThat() .contains( "Expected to be healthy after starting. The following services are not " + "running:"); @@ -614,7 +615,7 @@ public final Throwable failureCause() { assertThrows( IllegalArgumentException.class, () -> new ServiceManager(Arrays.asList(service1, service2))); - assertThat(expected.getMessage()).contains("started transitioning asynchronously"); + assertThat(expected).hasMessageThat().contains("started transitioning asynchronously"); } /** diff --git a/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java b/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java index f66b9ed47aa7..314b754aa833 100644 --- a/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/SimpleTimeLimiterTest.java @@ -158,7 +158,7 @@ public void testCallWithTimeout_badCallableWithEnoughTime() throws Exception { assertThrows( ExecutionException.class, () -> service.callWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleException.class); } public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception { @@ -181,7 +181,7 @@ public void testCallUninterruptiblyWithTimeout_badCallableWithEnoughTime() throw assertThrows( ExecutionException.class, () -> service.callUninterruptiblyWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleException.class); } public void testRunWithTimeout_goodRunnableWithEnoughTime() throws Exception { @@ -203,7 +203,7 @@ public void testRunWithTimeout_badRunnableWithEnoughTime() throws Exception { assertThrows( UncheckedExecutionException.class, () -> service.runWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleRuntimeException.class); } public void testRunUninterruptiblyWithTimeout_goodRunnableWithEnoughTime() throws Exception { @@ -225,7 +225,7 @@ public void testRunUninterruptiblyWithTimeout_badRunnableWithEnoughTime() throws assertThrows( UncheckedExecutionException.class, () -> service.runUninterruptiblyWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS)); - assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class); + assertThat(expected).hasCauseThat().isInstanceOf(SampleRuntimeException.class); } private interface Sample {