From 153b9c251e1406175f8a7c1f57c85d6fbca9c453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCllerleile?= Date: Fri, 10 Nov 2023 11:51:55 +0100 Subject: [PATCH] [pekko-testkit-typed] Support for JUnit5 in testkit (#751) * JUnit5 support for testkit typed * renamed TestKitJunitResource to TestKitJUnitResource * renamed "JUnit" to "Junit" to adhere to previous standard * Cleanup remaining residuals of renaming from JUnit to Junit * headers and scalafmt * Change Junit5 to JUnit5 * revert @unused annotation * scalafmt * javafmt * scalaify use of java optional * add newline * revert changes to Events.scala * replace Option.when with if statement --------- Co-authored-by: thomas --- .../typed/annotations/JUnit5TestKit.java | 25 ++++++ .../typed/javadsl/JUnit5TestKitBuilder.scala | 56 +++++++++++++ .../typed/javadsl/LogCapturingExtension.scala | 53 ++++++++++++ .../javadsl/TestKitJUnit5Extension.scala | 49 +++++++++++ .../javadsl/JUnit5IntegrationExampleTest.java | 60 +++++++++++++ .../LogCapturingExtensionExampleTest.java | 55 ++++++++++++ .../typed/javadsl/ActorTestKitJUnit5Test.java | 73 ++++++++++++++++ .../src/test/resources/application.conf | 3 + .../testkit/typed/scaladsl/GreeterMain.scala | 62 ++++++++++++++ .../scaladsl/JUnit5TestKitBuilderSpec.scala | 84 +++++++++++++++++++ project/Dependencies.scala | 5 ++ 11 files changed, 525 insertions(+) create mode 100644 actor-testkit-typed/src/main/java/org/apache/pekko/actor/testkit/typed/annotations/JUnit5TestKit.java create mode 100644 actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5TestKitBuilder.scala create mode 100644 actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtension.scala create mode 100644 actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/TestKitJUnit5Extension.scala create mode 100644 actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5IntegrationExampleTest.java create mode 100644 actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtensionExampleTest.java create mode 100644 actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKitJUnit5Test.java create mode 100644 actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/GreeterMain.scala create mode 100644 actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/JUnit5TestKitBuilderSpec.scala diff --git a/actor-testkit-typed/src/main/java/org/apache/pekko/actor/testkit/typed/annotations/JUnit5TestKit.java b/actor-testkit-typed/src/main/java/org/apache/pekko/actor/testkit/typed/annotations/JUnit5TestKit.java new file mode 100644 index 00000000000..cb6f7ed684d --- /dev/null +++ b/actor-testkit-typed/src/main/java/org/apache/pekko/actor/testkit/typed/annotations/JUnit5TestKit.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.actor.testkit.typed.annotations; + +import java.lang.annotation.*; + +@Documented +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface JUnit5TestKit {} diff --git a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5TestKitBuilder.scala b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5TestKitBuilder.scala new file mode 100644 index 00000000000..ac3ae80e6e9 --- /dev/null +++ b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5TestKitBuilder.scala @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.actor.testkit.typed.javadsl + +import org.apache.pekko +import com.typesafe.config.Config +import pekko.actor.testkit.typed.internal.TestKitUtils +import pekko.actor.testkit.typed.scaladsl.ActorTestKit.ApplicationTestConfig +import pekko.actor.typed.ActorSystem + +final class JUnit5TestKitBuilder() { + + var system: Option[ActorSystem[_]] = None + + var customConfig: Config = ApplicationTestConfig + + var name: String = TestKitUtils.testNameFromCallStack(classOf[JUnit5TestKitBuilder]) + + def withSystem(system: ActorSystem[_]): JUnit5TestKitBuilder = { + this.system = Some(system) + this + } + + def withCustomConfig(customConfig: Config): JUnit5TestKitBuilder = { + this.customConfig = customConfig + this + } + + def withName(name: String): JUnit5TestKitBuilder = { + this.name = name + this + } + + def build(): ActorTestKit = { + if (system.isDefined) { + return ActorTestKit.create(system.get) + } + ActorTestKit.create(name, customConfig) + } + +} diff --git a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtension.scala b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtension.scala new file mode 100644 index 00000000000..e4fd37c41eb --- /dev/null +++ b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtension.scala @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +package org.apache.pekko.actor.testkit.typed.javadsl + +import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation +import org.junit.jupiter.api.extension.{ ExtensionContext, InvocationInterceptor, ReflectiveInvocationContext } +import org.slf4j.LoggerFactory +import org.apache.pekko.actor.testkit.typed.internal.CapturingAppender + +import java.lang.reflect.Method +import scala.util.control.NonFatal + +final class LogCapturingExtension extends InvocationInterceptor { + + private val capturingAppender = CapturingAppender.get("") + + private val myLogger = LoggerFactory.getLogger(classOf[LogCapturing]) + + @throws[Throwable] + override def interceptTestMethod(invocation: Invocation[Void], invocationContext: ReflectiveInvocationContext[Method], + extensionContext: ExtensionContext): Unit = { + + val testClassName = invocationContext.getTargetClass.getSimpleName + val testMethodName = invocationContext.getExecutable.getName + + try { + myLogger.info(s"Logging started for test [${testClassName}: ${testMethodName}]") + invocation.proceed + myLogger.info( + s"Logging finished for test [${testClassName}: ${testMethodName}] that was successful") + } catch { + case NonFatal(e) => + println( + s"--> [${Console.BLUE}${testClassName}: ${testMethodName}${Console.RESET}] " + + s"Start of log messages of test that failed with ${e.getMessage}") + capturingAppender.flush() + println( + s"<-- [${Console.BLUE}${testClassName}: ${testMethodName}${Console.RESET}] " + + s"End of log messages of test that failed with ${e.getMessage}") + throw e + } finally { + + capturingAppender.clear() + } + } +} diff --git a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/TestKitJUnit5Extension.scala b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/TestKitJUnit5Extension.scala new file mode 100644 index 00000000000..50d63af7609 --- /dev/null +++ b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/TestKitJUnit5Extension.scala @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.actor.testkit.typed.javadsl + +import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit +import org.junit.jupiter.api.extension.{ AfterAllCallback, BeforeTestExecutionCallback, ExtensionContext } +import org.junit.platform.commons.support.AnnotationSupport + +final class TestKitJUnit5Extension() extends AfterAllCallback with BeforeTestExecutionCallback { + + var testKit: Option[ActorTestKit] = None + + /** + * Get a reference to the field annotated with @JUnit5Testkit [[JUnit5TestKit]] + */ + override def beforeTestExecution(context: ExtensionContext): Unit = { + val testInstance: Option[AnyRef] = + if (context.getTestInstance.isPresent) Some(context.getTestInstance.get()) else None + testInstance.map(instance => { + val annotations = AnnotationSupport.findAnnotatedFieldValues(instance, classOf[JUnit5TestKit]) + val fieldValue = annotations.stream().findFirst().orElseThrow(() => + throw new IllegalArgumentException("Could not find field annotated with @JUnit5TestKit")) + testKit = Some(fieldValue.asInstanceOf[ActorTestKit]) + }) + } + + /** + * Shutdown testKit + */ + override def afterAll(context: ExtensionContext): Unit = { + testKit.get.shutdownTestKit() + } + +} diff --git a/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5IntegrationExampleTest.java b/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5IntegrationExampleTest.java new file mode 100644 index 00000000000..7038d779528 --- /dev/null +++ b/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JUnit5IntegrationExampleTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +package jdocs.org.apache.pekko.actor.testkit.typed.javadsl; + +import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit; +import org.apache.pekko.actor.Address; +import org.apache.pekko.actor.testkit.typed.javadsl.*; +import org.apache.pekko.actor.testkit.typed.javadsl.JUnit5TestKitBuilder; +import org.apache.pekko.actor.typed.ActorRef; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +// #junit5-integration +@DisplayName("JUnit5") +@ExtendWith(TestKitJUnit5Extension.class) +class JUnit5IntegrationExampleTest { + + @JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build(); + + @Test + void junit5Test() { + Address address = testKit.system().address(); + assertNotNull(address); + } + + @Test + void testSomething() { + + ActorRef pinger = + testKit.spawn(AsyncTestingExampleTest.Echo.create(), "ping"); + TestProbe probe = testKit.createTestProbe(); + pinger.tell(new AsyncTestingExampleTest.Echo.Ping("hello", probe.ref())); + AsyncTestingExampleTest.Echo.Pong pong = + probe.expectMessage(new AsyncTestingExampleTest.Echo.Pong("hello")); + assertEquals("hello", pong.message); + } + + @Test + void testSomething2() { + ActorRef pinger2 = + testKit.spawn(AsyncTestingExampleTest.Echo.create(), "ping2"); + TestProbe probe2 = testKit.createTestProbe(); + pinger2.tell(new AsyncTestingExampleTest.Echo.Ping("hello", probe2.ref())); + AsyncTestingExampleTest.Echo.Pong pong = + probe2.expectMessage(new AsyncTestingExampleTest.Echo.Pong("hello")); + assertEquals("hello", pong.message); + } +} +// #junit5-integration diff --git a/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtensionExampleTest.java b/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtensionExampleTest.java new file mode 100644 index 00000000000..1ff8676555f --- /dev/null +++ b/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExtensionExampleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jdocs.org.apache.pekko.actor.testkit.typed.javadsl; + +import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit; +import org.apache.pekko.actor.testkit.typed.javadsl.*; +import org.apache.pekko.actor.testkit.typed.javadsl.JUnit5TestKitBuilder; +import org.apache.pekko.actor.typed.ActorRef; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import static jdocs.org.apache.pekko.actor.testkit.typed.javadsl.AsyncTestingExampleTest.Echo; + +// test code copied from LogCapturingExampleTest.java + +@DisplayName("JUnit5 log capturing") +@ExtendWith(TestKitJUnit5Extension.class) +@ExtendWith(LogCapturingExtension.class) +class LogCapturingExtensionExampleTest { + + @JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build(); + + @Test + void testSomething() { + ActorRef pinger = testKit.spawn(Echo.create(), "ping"); + TestProbe probe = testKit.createTestProbe(); + pinger.tell(new Echo.Ping("hello", probe.ref())); + probe.expectMessage(new Echo.Pong("hello")); + } + + @Test + void testSomething2() { + ActorRef pinger = testKit.spawn(Echo.create(), "ping"); + TestProbe probe = testKit.createTestProbe(); + pinger.tell(new Echo.Ping("hello", probe.ref())); + probe.expectMessage(new Echo.Pong("hello")); + } +} +// #log-capturing-junit5 diff --git a/actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKitJUnit5Test.java b/actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKitJUnit5Test.java new file mode 100644 index 00000000000..4e03cc0bbcd --- /dev/null +++ b/actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKitJUnit5Test.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +package org.apache.pekko.actor.testkit.typed.javadsl; + +import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit; +import org.apache.pekko.Done; +import org.apache.pekko.actor.typed.javadsl.Behaviors; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.scalatestplus.junit.JUnitSuite; + +import java.util.HashMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import static org.apache.pekko.Done.done; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@DisplayName("ActorTestKitTestJUnit5") +@ExtendWith(TestKitJUnit5Extension.class) +@ExtendWith(LogCapturingExtension.class) +class ActorTestKitJUnit5Test extends JUnitSuite { + + @JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build(); + + @Test + void systemNameShouldComeFromTestClassViaJunitResource() { + assertEquals("ActorTestKitJUnit5Test", testKit.system().name()); + } + + @Test + void systemNameShouldComeFromTestClass() { + final ActorTestKit testKit2 = ActorTestKit.create(); + try { + assertEquals("ActorTestKitJUnit5Test", testKit2.system().name()); + } finally { + testKit2.shutdownTestKit(); + } + } + + @Test + void systemNameShouldComeFromGivenClassName() { + final ActorTestKit testKit2 = ActorTestKit.create(HashMap.class.getName()); + try { + // removing package name and such + assertEquals("HashMap", testKit2.system().name()); + } finally { + testKit2.shutdownTestKit(); + } + } + + @Test + void testKitShouldSpawnActor() throws Exception { + final CompletableFuture started = new CompletableFuture<>(); + testKit.spawn( + Behaviors.setup( + (context) -> { + started.complete(done()); + return Behaviors.same(); + })); + assertNotNull(started.get(3, TimeUnit.SECONDS)); + } +} diff --git a/actor-testkit-typed/src/test/resources/application.conf b/actor-testkit-typed/src/test/resources/application.conf index 594856dc472..37fd4b4549f 100644 --- a/actor-testkit-typed/src/test/resources/application.conf +++ b/actor-testkit-typed/src/test/resources/application.conf @@ -2,3 +2,6 @@ # used by ActorTestKitSpec test.from-application = yes + +# used by JUnit5TestKitBuilderSpec +test.value = someValue diff --git a/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/GreeterMain.scala b/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/GreeterMain.scala new file mode 100644 index 00000000000..456298d3308 --- /dev/null +++ b/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/GreeterMain.scala @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +package org.apache.pekko.actor.testkit.typed.scaladsl + +import org.apache.pekko +import pekko.actor.typed.{ ActorRef, Behavior } +import pekko.actor.typed.scaladsl.Behaviors + +object Greeter { + final case class Greet(whom: String, replyTo: ActorRef[Greeted]) + final case class Greeted(whom: String, from: ActorRef[Greet]) + + def apply(): Behavior[Greet] = Behaviors.receive { (context, message) => + context.log.info("Hello {}!", message.whom) + // #greeter-send-messages + message.replyTo ! Greeted(message.whom, context.self) + // #greeter-send-messages + Behaviors.same + } +} + +object GreeterBot { + + def apply(max: Int): Behavior[Greeter.Greeted] = { + bot(0, max) + } + + private def bot(greetingCounter: Int, max: Int): Behavior[Greeter.Greeted] = + Behaviors.receive { (context, message) => + val n = greetingCounter + 1 + context.log.info("Greeting {} for {}", n, message.whom) + if (n == max) { + Behaviors.stopped + } else { + message.from ! Greeter.Greet(message.whom, context.self) + bot(n, max) + } + } +} + +object GreeterMain { + + final case class SayHello(name: String) + + def apply(): Behavior[SayHello] = + Behaviors.setup { context => + val greeter = context.spawn(Greeter(), "greeter") + + Behaviors.receiveMessage { message => + val replyTo = context.spawn(GreeterBot(max = 3), message.name) + greeter ! Greeter.Greet(message.name, replyTo) + Behaviors.same + } + } +} diff --git a/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/JUnit5TestKitBuilderSpec.scala b/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/JUnit5TestKitBuilderSpec.scala new file mode 100644 index 00000000000..15bc751dad4 --- /dev/null +++ b/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/JUnit5TestKitBuilderSpec.scala @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.actor.testkit.typed.scaladsl + +import org.apache.pekko +import pekko.actor.typed.ActorSystem +import com.typesafe.config.ConfigFactory +import org.apache.pekko.actor.testkit.typed.javadsl.JUnit5TestKitBuilder +import org.scalatest.wordspec.AnyWordSpec + +class JUnit5TestKitBuilderSpec extends AnyWordSpec { + + "the JUnit5TestKitBuilder" should { + "create a Testkit with name hello" in { + val actualTestKit = new JUnit5TestKitBuilder().withName("hello").build() + + assertResult("hello")(actualTestKit.system.name) + } + } + + "the JUnit5TestKitBuilder" should { + "create a Testkit with the classname as name" in { + val actualTestKit = new JUnit5TestKitBuilder() + .build() + + assertResult("JUnit5TestKitBuilderSpec")(actualTestKit.system.name) + } + } + + "the JUnit5TestKitBuilder" should { + "create a Testkit with a custom config" in { + + val conf = ConfigFactory.load("application.conf") + val actualTestKit = new JUnit5TestKitBuilder() + .withCustomConfig(conf) + .build() + assertResult("someValue")(actualTestKit.system.settings.config.getString("test.value")) + assertResult("JUnit5TestKitBuilderSpec")(actualTestKit.system.name) + + } + } + + "the JUnit5TestKitBuilder" should { + "create a Testkit with a custom config and name" in { + + val conf = ConfigFactory.load("application.conf") + val actualTestKit = new JUnit5TestKitBuilder() + .withCustomConfig(conf) + .withName("hello") + .build() + assertResult("someValue")(actualTestKit.system.settings.config.getString("test.value")) + assertResult("hello")(actualTestKit.system.name) + + } + } + + "the JUnit5TestKitBuilder" should { + "create a Testkit with a custom system" in { + + val system: ActorSystem[GreeterMain.SayHello] = ActorSystem(GreeterMain(), "AkkaQuickStart") + + val actualTestKit = new JUnit5TestKitBuilder() + .withSystem(system) + .build() + assertResult("AkkaQuickStart")(actualTestKit.system.name) + } + } + +} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index aec93fcfbde..3b03ccdd0c4 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -22,6 +22,7 @@ object Dependencies { .withRank(KeyRanks.Invisible) // avoid 'unused key' warning val junitVersion = "4.13.2" + val junit5Version = "5.10.0" val slf4jVersion = "2.0.9" // check agrona version when updating this val aeronVersion = "1.42.1" @@ -83,6 +84,7 @@ object Dependencies { val lmdb = "org.lmdbjava" % "lmdbjava" % "0.8.3" val junit = "junit" % "junit" % junitVersion + val junit5 = "org.junit.jupiter" % "junit-jupiter-engine" % junit5Version // For Java 8 Conversions val java8Compat = Def.setting { @@ -136,6 +138,7 @@ object Dependencies { val commonsCodec = "commons-codec" % "commons-codec" % "1.16.0" % Test val commonsCompress = "org.apache.commons" % "commons-compress" % "1.24.0" % Test val junit = "junit" % "junit" % junitVersion % Test + val junit5 = "org.junit.jupiter" % "junit-jupiter-engine" % junit5Version % Test val httpClient = "org.apache.httpcomponents" % "httpclient" % "4.5.14" % Test val logback = Compile.logback % Test @@ -209,6 +212,7 @@ object Dependencies { val levelDBNative = "org.fusesource.leveldbjni" % "leveldbjni-all" % "1.8" % "optional;provided" val junit = Compile.junit % "optional;provided;test" + val junit5 = Compile.junit5 % "optional;provided;test" val scalatest = Def.setting { "org.scalatest" %% "scalatest" % scalaTestVersion % "optional;provided;test" } @@ -262,6 +266,7 @@ object Dependencies { val actorTestkitTyped = l ++= Seq( Provided.logback, Provided.junit, + Provided.junit5, Provided.scalatest.value, TestDependencies.scalatestJUnit.value)