如何使用akka-http-testkit创建测试?

时间:2016-12-23 20:42:01

标签: akka akka-http akka-testkit

我正在尝试使用 akka.http.scaladsl.testkit.ScalatestRouteTest 创建一个简单的测试,结果是:

class FooRoutesSpec extends FlatSpec with Matchers with ScalatestRouteTest with FooResource {

  Get(s"/foos/1") ~> fooRoutes ~> check {
      status shouldBe OK
    }
}

UPDATE stacktrace

An exception or error caused a run to abort: akka.http.scaladsl.server.RequestContextImpl.<init>(Lakka/http/scaladsl/model/HttpRequest;Lakka/event/LoggingAdapter;Lakka/http/scaladsl/settings/RoutingSettings;Lscala/concurrent/ExecutionContextExecutor;Lakka/stream/Materializer;)V 
java.lang.NoSuchMethodError: akka.http.scaladsl.server.RequestContextImpl.<init>(Lakka/http/scaladsl/model/HttpRequest;Lakka/event/LoggingAdapter;Lakka/http/scaladsl/settings/RoutingSettings;Lscala/concurrent/ExecutionContextExecutor;Lakka/stream/Materializer;)V
    at akka.http.scaladsl.testkit.RouteTest$TildeArrow$$anon$1.apply(RouteTest.scala:158)
    at akka.http.scaladsl.testkit.RouteTest$TildeArrow$$anon$1.apply(RouteTest.scala:150)
    at akka.http.scaladsl.testkit.RouteTest$WithTransformation2.$tilde$greater(RouteTest.scala:125)
    at routes.foo.MyRoutesSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(MyRoutesSpec.scala:34)
    at routes.foo.MyRoutesSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(MyRoutesSpec.scala:34)
    at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    at org.scalatest.Transformer.apply(Transformer.scala:20)
    at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078)
    at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
    at org.scalatest.WordSpec.withFixture(WordSpec.scala:1881)
    at org.scalatest.WordSpecLike$class.invokeWithFixture$1(WordSpecLike.scala:1075)
    at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
    at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
    at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
    at org.scalatest.WordSpecLike$class.runTest(WordSpecLike.scala:1088)
    at org.scalatest.WordSpec.runTest(WordSpec.scala:1881)
    at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
    at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
    at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:373)
    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:410)
    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
    at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
    at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
    at org.scalatest.WordSpecLike$class.runTests(WordSpecLike.scala:1147)
    at org.scalatest.WordSpec.runTests(WordSpec.scala:1881)
    at org.scalatest.Suite$class.run(Suite.scala:1147)
    at org.scalatest.WordSpec.org$scalatest$WordSpecLike$$super$run(WordSpec.scala:1881)
    at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
    at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
    at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
    at org.scalatest.WordSpecLike$class.run(WordSpecLike.scala:1192)
    at routes.foo.MyRoutesSpec.org$scalatest$BeforeAndAfterAll$$super$run(MyRoutesSpec.scala:13)
    at org.scalatest.BeforeAndAfterAll$class.liftedTree1$1(BeforeAndAfterAll.scala:213)
    at org.scalatest.BeforeAndAfterAll$class.run(BeforeAndAfterAll.scala:210)
    at routes.foo.MyRoutesSpec.run(MyRoutesSpec.scala:13)
    at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
    at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340)
    at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1334)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1334)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
    at org.scalatest.tools.Runner$.run(Runner.scala:850)
    at org.scalatest.tools.Runner.run(Runner.scala)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

build.sbt

libraryDependencies ++= {
  val Json4sVersion     = "3.2.11"
  val akkaV       = "2.4.16"
  val akkaHttpV   = "3.0.0-RC1"
  val scalaTestV  = "3.0.1"
  Seq(
    "com.typesafe.akka" %% "akka-slf4j"                 % akkaV,
    "com.typesafe.akka" %% "akka-http"                  % akkaHttpV,
    "com.typesafe.akka" %% "akka-http-testkit"          % akkaHttpV,
    "com.typesafe.akka" %% "akka-testkit"               % akkaV,
    "ch.qos.logback"    %  "logback-classic"            % "1.1.2",
    "org.json4s"        %% "json4s-native"              % Json4sVersion,
    "org.json4s"        %% "json4s-ext"                 % Json4sVersion,
    "de.heikoseeberger" %% "akka-http-json4s"           % "1.4.2",
    "org.scalatest"     %% "scalatest"                  % scalaTestV % "test"
  )
}

我能看到 RequestContextImpl 需要 RequestContext

package akka.http.impl.server
    private[http] final case class RequestContextImpl(val underlying : akka.http.scaladsl.server.RequestContext) extends scala.AnyRef with akka.http.javadsl.server.RequestContext

但RequestContext是一个类而不是trait

package akka.http.javadsl.server
class RequestContex

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

akka-http-json4s正在拖动旧版本的Akka-HTTP(即根据config的2.0.1)。

最新的akka-http-json4s版本1.11.0。这使用Akka-HTTP 10.0.0和Akka 2.4.14(根据config)。

尝试碰撞所有依赖项:

  val akkaV       = "2.4.14"
  val akkaHttpV   = "10.0.0"
  val akkaHttpJson= "1.11.0"

"de.heikoseeberger" %% "akka-http-json4s" % akkaHttpJson中使用的最后一个版本)