How to create tests with using akka-http-testkit?

972 views Asked by At

I am trying to create a simple test using akka.http.scaladsl.testkit.ScalatestRouteTest and the result is:

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"
  )
}

What I could see is that RequestContextImpl requires a 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

but the RequestContext is a class instead of trait

package akka.http.javadsl.server
class RequestContex

Any idea how to solve this issue?

1

There are 1 answers

3
Stefano Bonetti On

akka-http-json4s is dragging in an older version of Akka-HTTP (namely 2.0.1 as per config).

There is a far more recent akka-http-json4s, version 1.11.0. This uses Akka-HTTP 10.0.0 and Akka 2.4.14 (as per config).

Try bumping all of your dependencies:

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

(last version to be used in "de.heikoseeberger" %% "akka-http-json4s" % akkaHttpJson)