问题
i have a test class in which i need to use both akka testkit and akka http test kit
so i am doing it like this
class MyTest extends TestKit(ActorSystem("testsys")) with ScalaFutures with ImplicitSender with AnyWordSpecLike with Matchers with BeforeAndAfterAll with ScalatestRouteTest {
//tests here
}
but i am getting a compile time error
implicit val system: akka.actor.ActorSystem (defined in class TestKit) and
[error] implicit val system: akka.actor.ActorSystem (defined in trait RouteTest)
[error] (note: this can be resolved by declaring an `override` in class MyTest.)
[error] class InsertEventTest extends TestKit(ActorSystem("testinterpret")) with ScalaFutures with ImplicitSender with AnyWordSpecLike with Matchers with BeforeAndAfterAll with ScalatestRouteTest {
[error] ^
(note: this can be resolved by declaring an `override` in class InsertEventTest.)
so i did something like this
class MyTest extends TestKit(ActorSystem("testsys")) with ScalaFutures with ImplicitSender with AnyWordSpecLike with Matchers with BeforeAndAfterAll with ScalatestRouteTest {
override def createActorSystem(): ActorSystem = system
override implicit val system: ActorSystem = createActorSystem
//tests here
}
but i am getting NullPointerException
Java.lang.NullPointerException: system must not be null!
[info] at java.base/java.util.Objects.requireNonNull(Objects.java:246)
[info] at akka.actor.ExtensionId.apply(Extension.scala:78)
[info] at akka.actor.ExtensionId.apply$(Extension.scala:77)
[info] at akka.testkit.TestKitExtension$.apply(TestKitExtension.scala:14)
[info] at akka.testkit.TestKitBase.$init$(TestKit.scala:161)
[info] at akka.testkit.TestKit.<init>(TestKit.scala:928)
[info] at MyTest.<init>(MyTest.scala:40)
[info] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[info] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
here is the documents of TestKit and RouteTest
来源:https://stackoverflow.com/questions/60653626/how-to-resolve-conflict-of-actorsystem-in-akka-http-test-and-akka-test-kit