how to resolve conflict of ActorSystem in akka http test and akka test kit

雨燕双飞 提交于 2020-12-26 11:08:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!