Refactor exception handling in StatusReplyTest (#30081)

* Refactor exception handling in StatusReplyTest

* Change messages in assertThrows
This commit is contained in:
Andrei Arlou 2021-03-12 13:46:20 +03:00 committed by GitHub
parent d19855542b
commit 23f703fa08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 45 deletions

View file

@ -30,53 +30,41 @@ public class StatusReplyTest extends JUnitSuite {
new AkkaJUnitActorSystemResource("JavaAPI", AkkaSpec.testConf()); new AkkaJUnitActorSystemResource("JavaAPI", AkkaSpec.testConf());
@Test @Test
public void testSuccessApi() { public void successReplyThrowsExceptionWhenGetErrorIsCalled() {
StatusReply<String> reply = StatusReply.success("woho"); StatusReply<String> reply = StatusReply.success("woho");
assertTrue(reply.isSuccess()); assertTrue(reply.isSuccess());
assertFalse(reply.isError()); assertFalse(reply.isError());
assertEquals("woho", reply.getValue()); assertEquals("woho", reply.getValue());
try { Assert.assertThrows(
reply.getError(); "Calling .getError() on success should throw",
Assert.fail("Calling get error on success did not throw"); IllegalArgumentException.class,
} catch (IllegalArgumentException ex) { reply::getError);
// this is what we expect
}
} }
@Test @Test
public void testErrorMessageApi() { public void failedReplyThrowsExceptionWhenGetValueIsCalled() {
StatusReply<String> reply = StatusReply.error("boho"); StatusReply<String> reply = StatusReply.error("boho");
assertTrue(reply.isError()); assertTrue(reply.isError());
assertFalse(reply.isSuccess()); assertFalse(reply.isSuccess());
assertEquals("boho", reply.getError().getMessage()); assertEquals("boho", reply.getError().getMessage());
try { Assert.assertThrows(
reply.getValue(); "Calling .getValue() on error should throw",
Assert.fail("Calling get value on error did not throw"); StatusReply.ErrorMessage.class,
} catch (StatusReply.ErrorMessage ex) { reply::getValue);
// this is what we expect
} catch (Throwable th) {
Assert.fail("Unexpected exception type: " + th);
}
} }
@Test @Test
public void testErrorExceptionApi() { public void failedReplyThrowsOriginalExceptionWhenGetValueIsCalled() {
StatusReply<String> reply = StatusReply.error(new TestException("boho")); StatusReply<String> reply = StatusReply.error(new TestException("boho"));
assertTrue(reply.isError()); assertTrue(reply.isError());
assertFalse(reply.isSuccess()); assertFalse(reply.isSuccess());
assertEquals("boho", reply.getError().getMessage()); assertEquals("boho", reply.getError().getMessage());
try { Assert.assertThrows(
reply.getValue(); "Calling .getValue() on error should throw", TestException.class, 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);
}
} }
@Test @Test
public void testAskWithStatusSuccess() throws Exception { public void askWithStatusSuccessReturnsValue() throws Exception {
TestProbe probe = new TestProbe(actorSystemResource.getSystem()); TestProbe probe = new TestProbe(actorSystemResource.getSystem());
CompletionStage<Object> response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3)); CompletionStage<Object> response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3));
@ -88,36 +76,32 @@ public class StatusReplyTest extends JUnitSuite {
} }
@Test @Test
public void testAskWithStatusErrorMessage() throws Exception { public void askWithStatusErrorReturnsErrorMessageExceptionForText() {
TestProbe probe = new TestProbe(actorSystemResource.getSystem()); TestProbe probe = new TestProbe(actorSystemResource.getSystem());
CompletionStage<Object> response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3)); CompletionStage<Object> response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3));
probe.expectMsg("request"); probe.expectMsg("request");
probe.lastSender().tell(StatusReply.error("boho"), ActorRef.noSender()); probe.lastSender().tell(StatusReply.error("boho"), ActorRef.noSender());
ExecutionException ex =
try { Assert.assertThrows(
Object result = response.toCompletableFuture().get(3, TimeUnit.SECONDS); ExecutionException.class,
} catch (ExecutionException ex) { () -> response.toCompletableFuture().get(3, TimeUnit.SECONDS));
// what we expected assertEquals(StatusReply.ErrorMessage.class, ex.getCause().getClass());
assertEquals(StatusReply.ErrorMessage.class, ex.getCause().getClass()); assertEquals("boho", ex.getCause().getMessage());
assertEquals("boho", ex.getCause().getMessage());
}
} }
@Test @Test
public void testAskWithStatusErrorException() throws Exception { public void askWithStatusErrorReturnsOriginalException() {
TestProbe probe = new TestProbe(actorSystemResource.getSystem()); TestProbe probe = new TestProbe(actorSystemResource.getSystem());
CompletionStage<Object> response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3)); CompletionStage<Object> response = askWithStatus(probe.ref(), "request", Duration.ofSeconds(3));
probe.expectMsg("request"); probe.expectMsg("request");
probe.lastSender().tell(StatusReply.error(new TestException("boho")), ActorRef.noSender()); probe.lastSender().tell(StatusReply.error(new TestException("boho")), ActorRef.noSender());
ExecutionException ex =
try { Assert.assertThrows(
Object result = response.toCompletableFuture().get(3, TimeUnit.SECONDS); ExecutionException.class,
} catch (ExecutionException ex) { () -> response.toCompletableFuture().get(3, TimeUnit.SECONDS));
// what we expected assertEquals(TestException.class, ex.getCause().getClass());
assertEquals(TestException.class, ex.getCause().getClass()); assertEquals("boho", ex.getCause().getMessage());
assertEquals("boho", ex.getCause().getMessage());
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com> * Copyright (C) 2009-2021 Lightbend Inc. <https://www.lightbend.com>
*/ */
package akka.cluster.singleton package akka.cluster.singleton