diff --git a/akka-actor-tests/src/test/java/akka/pattern/StatusReplyTest.java b/akka-actor-tests/src/test/java/akka/pattern/StatusReplyTest.java index 9a0ea47344..1543374d60 100644 --- a/akka-actor-tests/src/test/java/akka/pattern/StatusReplyTest.java +++ b/akka-actor-tests/src/test/java/akka/pattern/StatusReplyTest.java @@ -30,53 +30,41 @@ public class StatusReplyTest extends JUnitSuite { new AkkaJUnitActorSystemResource("JavaAPI", AkkaSpec.testConf()); @Test - public void testSuccessApi() { + public void successReplyThrowsExceptionWhenGetErrorIsCalled() { StatusReply reply = StatusReply.success("woho"); assertTrue(reply.isSuccess()); assertFalse(reply.isError()); assertEquals("woho", reply.getValue()); - try { - reply.getError(); - Assert.fail("Calling get error on success did not throw"); - } catch (IllegalArgumentException ex) { - // this is what we expect - } + Assert.assertThrows( + "Calling .getError() on success should throw", + IllegalArgumentException.class, + reply::getError); } @Test - public void testErrorMessageApi() { + public void failedReplyThrowsExceptionWhenGetValueIsCalled() { StatusReply reply = StatusReply.error("boho"); assertTrue(reply.isError()); assertFalse(reply.isSuccess()); assertEquals("boho", reply.getError().getMessage()); - try { - reply.getValue(); - Assert.fail("Calling get value on error did not throw"); - } catch (StatusReply.ErrorMessage ex) { - // this is what we expect - } catch (Throwable th) { - Assert.fail("Unexpected exception type: " + th); - } + Assert.assertThrows( + "Calling .getValue() on error should throw", + StatusReply.ErrorMessage.class, + reply::getValue); } @Test - public void testErrorExceptionApi() { + public void failedReplyThrowsOriginalExceptionWhenGetValueIsCalled() { StatusReply reply = StatusReply.error(new TestException("boho")); assertTrue(reply.isError()); assertFalse(reply.isSuccess()); assertEquals("boho", reply.getError().getMessage()); - try { - reply.getValue(); - Assert.fail("Calling get value on error did not throw"); - } catch (TestException ex) { - // this is what we expect - } catch (Throwable th) { - Assert.fail("Unexpected exception type: " + th); - } + Assert.assertThrows( + "Calling .getValue() on error should throw", TestException.class, reply::getValue); } @Test - public void testAskWithStatusSuccess() throws Exception { + public void askWithStatusSuccessReturnsValue() throws Exception { TestProbe probe = new TestProbe(actorSystemResource.getSystem()); CompletionStage response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3)); @@ -88,36 +76,32 @@ public class StatusReplyTest extends JUnitSuite { } @Test - public void testAskWithStatusErrorMessage() throws Exception { + public void askWithStatusErrorReturnsErrorMessageExceptionForText() { TestProbe probe = new TestProbe(actorSystemResource.getSystem()); CompletionStage response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3)); probe.expectMsg("request"); probe.lastSender().tell(StatusReply.error("boho"), ActorRef.noSender()); - - try { - Object result = response.toCompletableFuture().get(3, TimeUnit.SECONDS); - } catch (ExecutionException ex) { - // what we expected - assertEquals(StatusReply.ErrorMessage.class, ex.getCause().getClass()); - assertEquals("boho", ex.getCause().getMessage()); - } + ExecutionException ex = + Assert.assertThrows( + ExecutionException.class, + () -> response.toCompletableFuture().get(3, TimeUnit.SECONDS)); + assertEquals(StatusReply.ErrorMessage.class, ex.getCause().getClass()); + assertEquals("boho", ex.getCause().getMessage()); } @Test - public void testAskWithStatusErrorException() throws Exception { + public void askWithStatusErrorReturnsOriginalException() { TestProbe probe = new TestProbe(actorSystemResource.getSystem()); CompletionStage response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3)); probe.expectMsg("request"); probe.lastSender().tell(StatusReply.error(new TestException("boho")), ActorRef.noSender()); - - try { - Object result = response.toCompletableFuture().get(3, TimeUnit.SECONDS); - } catch (ExecutionException ex) { - // what we expected - assertEquals(TestException.class, ex.getCause().getClass()); - assertEquals("boho", ex.getCause().getMessage()); - } + ExecutionException ex = + Assert.assertThrows( + ExecutionException.class, + () -> response.toCompletableFuture().get(3, TimeUnit.SECONDS)); + assertEquals(TestException.class, ex.getCause().getClass()); + assertEquals("boho", ex.getCause().getMessage()); } } diff --git a/akka-cluster-tools/src/multi-jvm/scala/akka/cluster/singleton/ClusterSingletonManagerPreparingForShutdownSpec.scala b/akka-cluster-tools/src/multi-jvm/scala/akka/cluster/singleton/ClusterSingletonManagerPreparingForShutdownSpec.scala index a818406142..1565db4983 100644 --- a/akka-cluster-tools/src/multi-jvm/scala/akka/cluster/singleton/ClusterSingletonManagerPreparingForShutdownSpec.scala +++ b/akka-cluster-tools/src/multi-jvm/scala/akka/cluster/singleton/ClusterSingletonManagerPreparingForShutdownSpec.scala @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2020 Lightbend Inc. + * Copyright (C) 2009-2021 Lightbend Inc. */ package akka.cluster.singleton