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());
@Test
public void testSuccessApi() {
public void successReplyThrowsExceptionWhenGetErrorIsCalled() {
StatusReply<String> 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<String> 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<String> 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<Object> 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<Object> 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
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<Object> 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
ExecutionException ex =
Assert.assertThrows(
ExecutionException.class,
() -> response.toCompletableFuture().get(3, TimeUnit.SECONDS));
assertEquals(TestException.class, ex.getCause().getClass());
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