diff --git a/core/src/test/java/com/google/common/truth/ExpectTest.java b/core/src/test/java/com/google/common/truth/ExpectTest.java index 037242229..28510a0d2 100644 --- a/core/src/test/java/com/google/common/truth/ExpectTest.java +++ b/core/src/test/java/com/google/common/truth/ExpectTest.java @@ -182,11 +182,11 @@ public void warnWhenExpectIsNotRule() { public void bash() throws Exception { Runnable task = () -> expect.that(3).isEqualTo(4); List> results = new ArrayList<>(); - ExecutorService executor = newFixedThreadPool(10); - for (int i = 0; i < 1000; i++) { - results.add(executor.submit(task)); + try (ExecutorService executor = newFixedThreadPool(10)) { + for (int i = 0; i < 1000; i++) { + results.add(executor.submit(task)); + } } - executor.shutdown(); for (Future result : results) { result.get(); } @@ -195,33 +195,33 @@ public void bash() throws Exception { @Test public void failWhenCallingThatAfterTest() { - ExecutorService executor = newSingleThreadExecutor(); - taskToAwait = - executor.submit( - () -> { - awaitUninterruptibly(testMethodComplete); - assertThrows(IllegalStateException.class, () -> expect.that(3)); - }); - executor.shutdown(); + try (ExecutorService executor = newSingleThreadExecutor()) { + taskToAwait = + executor.submit( + () -> { + awaitUninterruptibly(testMethodComplete); + assertThrows(IllegalStateException.class, () -> expect.that(3)); + }); + } } @Test public void failWhenCallingFailingAssertionMethodAfterTest() { - ExecutorService executor = newSingleThreadExecutor(); - /* - * We wouldn't expect people to do this exactly. The point is that, if someone were to call - * expect.that(3).isEqualTo(4), we would always either fail the test or throw an - * IllegalStateException, not record a "failure" that we never read. - */ - IntegerSubject expectThat3 = expect.that(3); - taskToAwait = - executor.submit( - () -> { - awaitUninterruptibly(testMethodComplete); - IllegalStateException expected = - assertThrows(IllegalStateException.class, () -> expectThat3.isEqualTo(4)); - assertThat(expected).hasCauseThat().isInstanceOf(AssertionError.class); - }); - executor.shutdown(); + try (ExecutorService executor = newSingleThreadExecutor()) { + /* + * We wouldn't expect people to do this exactly. The point is that, if someone were to call + * expect.that(3).isEqualTo(4), we would always either fail the test or throw an + * IllegalStateException, not record a "failure" that we never read. + */ + IntegerSubject expectThat3 = expect.that(3); + taskToAwait = + executor.submit( + () -> { + awaitUninterruptibly(testMethodComplete); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> expectThat3.isEqualTo(4)); + assertThat(expected).hasCauseThat().isInstanceOf(AssertionError.class); + }); + } } }