From 1a36631dbbe3eb36e7796f2cadf8d2697ab45a8f Mon Sep 17 00:00:00 2001 From: Andrei Arlou Date: Wed, 5 May 2021 11:45:37 +0300 Subject: [PATCH] Use assertThrows in classes ActorCreationTest and OutputStreamSinkTest (#30165) --- .../java/akka/actor/ActorCreationTest.java | 86 +++++++++---------- .../akka/stream/io/OutputStreamSinkTest.java | 27 +++--- 2 files changed, 56 insertions(+), 57 deletions(-) diff --git a/akka-actor-tests/src/test/java/akka/actor/ActorCreationTest.java b/akka-actor-tests/src/test/java/akka/actor/ActorCreationTest.java index 1deb70f570..a319544bcc 100644 --- a/akka-actor-tests/src/test/java/akka/actor/ActorCreationTest.java +++ b/akka-actor-tests/src/test/java/akka/actor/ActorCreationTest.java @@ -10,6 +10,7 @@ import static java.util.stream.Collectors.toCollection; import java.util.ArrayList; import java.util.stream.IntStream; +import org.junit.Assert; import org.junit.Test; import akka.japi.Creator; @@ -196,14 +197,11 @@ public class ActorCreationTest extends JUnitSuite { @SuppressWarnings("unchecked") @Deprecated public void testWrongErasedStaticCreator() { - try { - Props.create(new G()); - assert false; - } catch (IllegalArgumentException e) { - assertEquals( - "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", - e.getMessage()); - } + IllegalArgumentException exception = + Assert.assertThrows(IllegalArgumentException.class, () -> Props.create(new G())); + assertEquals( + "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", + exception.getMessage()); Props.create(AbstractActor.class, new G()); } @@ -217,14 +215,16 @@ public class ActorCreationTest extends JUnitSuite { @Test @Deprecated public void testWrongAnonymousClassStaticCreator() { - try { - Props.create(new C() {}); // has implicit reference to outer class - org.junit.Assert.fail("Should have detected this is not a real static class, and thrown"); - } catch (IllegalArgumentException e) { - assertEquals( - "cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", - e.getMessage()); - } + IllegalArgumentException exception = + Assert.assertThrows( + "Should have detected this is not a real static class, and thrown", + IllegalArgumentException.class, + () -> { + Props.create(new C() {}); // has implicit reference to outer class + }); + assertEquals( + "cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", + exception.getMessage()); } @Test @@ -257,14 +257,11 @@ public class ActorCreationTest extends JUnitSuite { @Test public void testWrongAbstractActorClass() { - try { - Props.create(H.class, "a"); - assert false; - } catch (IllegalArgumentException e) { - assertEquals( - String.format("Actor class [%s] must not be abstract", H.class.getName()), - e.getMessage()); - } + IllegalArgumentException exception = + Assert.assertThrows(IllegalArgumentException.class, () -> Props.create(H.class, "a")); + assertEquals( + String.format("Actor class [%s] must not be abstract", H.class.getName()), + exception.getMessage()); } private static Creator createAnonymousCreatorInStaticMethod() { @@ -294,17 +291,19 @@ public class ActorCreationTest extends JUnitSuite { @Test @Deprecated public void testAnonymousClassCreatorWithArguments() { - try { - final Creator anonymousCreatorFromStaticMethod = new P("hello") { - // captures enclosing class - }; - Props.create(anonymousCreatorFromStaticMethod); - org.junit.Assert.fail("Should have detected this is not a real static class, and thrown"); - } catch (IllegalArgumentException e) { - assertEquals( - "cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", - e.getMessage()); - } + IllegalArgumentException exception = + Assert.assertThrows( + "Should have detected this is not a real static class, and thrown", + IllegalArgumentException.class, + () -> { + final Creator anonymousCreatorFromStaticMethod = new P("hello") { + // captures enclosing class + }; + Props.create(anonymousCreatorFromStaticMethod); + }); + assertEquals( + "cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", + exception.getMessage()); } @Test @@ -318,14 +317,15 @@ public class ActorCreationTest extends JUnitSuite { public void testWrongPropsUsingLambdaWithoutClass() { final Props p = TestActor.propsUsingLamda(17); assertEquals(TestActor.class, p.actorClass()); - try { - TestActor.propsUsingLamdaWithoutClass(17); - org.junit.Assert.fail("Should have detected lambda erasure, and thrown"); - } catch (IllegalArgumentException e) { - assertEquals( - "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", - e.getMessage()); - } + + IllegalArgumentException exception = + Assert.assertThrows( + "Should have detected lambda erasure, and thrown", + IllegalArgumentException.class, + () -> TestActor.propsUsingLamdaWithoutClass(17)); + assertEquals( + "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", + exception.getMessage()); } @Test diff --git a/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java b/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java index e7c4b7dd72..ac5217fa0d 100644 --- a/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java @@ -15,18 +15,13 @@ import akka.util.ByteString; import org.junit.Assert; import org.junit.ClassRule; import org.junit.Test; -import org.junit.matchers.JUnitMatchers; -import scala.concurrent.Await; -import scala.concurrent.Future; -import scala.concurrent.duration.FiniteDuration; import java.io.OutputStream; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; public class OutputStreamSinkTest extends StreamTest { public OutputStreamSinkTest() { @@ -38,7 +33,7 @@ public class OutputStreamSinkTest extends StreamTest { new AkkaJUnitActorSystemResource("OutputStreamSinkTest", Utils.UnboundedMailboxConfig()); @Test - public void mustSignalFailureViaFailingFuture() throws Exception { + public void mustSignalFailureViaFailingFuture() { final OutputStream os = new OutputStream() { @@ -54,12 +49,16 @@ public class OutputStreamSinkTest extends StreamTest { final CompletionStage resultFuture = Source.single(ByteString.fromString("123456")) .runWith(StreamConverters.fromOutputStream(() -> os), system); - try { - resultFuture.toCompletableFuture().get(3, TimeUnit.SECONDS); - Assert.fail("expected IOIncompleteException"); - } catch (ExecutionException e) { - Assert.assertEquals(e.getCause().getClass(), IOOperationIncompleteException.class); - Assert.assertEquals(e.getCause().getCause().getMessage(), "Can't accept more data."); - } + + ExecutionException exception = + Assert.assertThrows( + "CompletableFuture.get() should throw ExecutionException", + ExecutionException.class, + () -> resultFuture.toCompletableFuture().get(3, TimeUnit.SECONDS)); + assertEquals( + "The cause of ExecutionException should be IOOperationIncompleteException", + exception.getCause().getClass(), + IOOperationIncompleteException.class); + assertEquals(exception.getCause().getCause().getMessage(), "Can't accept more data."); } }