Reputation: 3939
I have a Play 2.7.x application that should forward a request to a different URL. It has a controller implemented like:
import play.api.mvc._
import play.api.libs.ws.WSClient
class MyController(val controllerComponents: ControllerComponents, ws: WSClient, baseUrl: String) extends BaseController {
// POST /foo routed here
def foo() = {
Action.async { request =>
ws.url(s"http://$baseUrl/foo").post(equest.body.asJson.get).map(_ -> Ok(""))
}
}
}
I now want to create a test that makes a fake request, and uses a fake server to test everything. Following https://www.playframework.com/documentation/2.7.x/ScalaTestingWebServiceClients I tried the following:
class ControllersSpec extends PlaySpec with OneAppPerSuiteWithComponents {
override lazy val components = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents with AhcWSComponents {
lazy val myController = new MyController(controllerComponents, wsClient, "localhost:19001")
lazy val router = new Routes(httpErrorHandler, myController)
}
"My Controller" should {
"forward requests" in {
Server.withRouterFromComponents(ServerConfig(port=Some(19001))) {
import play.api.routing.sird._
routes: BuiltInComponents => {
case POST(p"/foo") =>
routes.defaultActionBuilder {
Ok("")
}
}
} { port =>
route(app, FakeRequest(POST, "/foo", FakeHeaders(), "{\"foo\": \"bar\"}") // <-- line 86
}
}
}
}
However, trying to run this results in:
java.lang.IllegalStateException: cannot create children while terminating or terminated
at akka.actor.dungeon.Children$class.makeChild(Children.scala:270)
at akka.actor.dungeon.Children$class.attachChild(Children.scala:48)
at akka.actor.ActorCell.attachChild(ActorCell.scala:370)
at akka.stream.impl.ExtendedActorMaterializer.actorOf(ActorMaterializerImpl.scala:60)
at akka.stream.impl.GraphStageIsland.onIslandReady(PhasedFusingActorMaterializer.scala:742)
at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:507)
at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:420)
at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:415)
at akka.stream.scaladsl.RunnableGraph.run(Flow.scala:496)
at akka.stream.scaladsl.Source.runWith(Source.scala:83)
at play.api.libs.streams.StrictAccumulator.run(Accumulator.scala:203)
at play.api.test.EssentialActionCaller$class.call(Helpers.scala:249)
at play.api.test.Helpers$.call(Helpers.scala:601)
at play.api.test.RouteInvokers$class.route(Helpers.scala:271)
at play.api.test.Helpers$.route(Helpers.scala:601)
at play.api.test.RouteInvokers$class.route(Helpers.scala:281)
at play.api.test.Helpers$.route(Helpers.scala:601)
at controllers.ControllersSpec$$anonfun$1$$anonfun$apply$mcV$sp$2$$anonfun$3.apply(ControllersSpec.scala:86)
I assume that the test server's and the app's usage of Akka is interfering, and that one terminates things prematurely for the other, but I don't know their workings well enough to understand it fully. How would I set up a test like this correctly?
Upvotes: 0
Views: 1093
Reputation: 350
If you don't mind using an external library, I'd suggest two of them:
It's probably what you want, to set it up you need just to:
val mockWs = MockWS {
case ("POST", "/foo") => Action { Ok(Json.parse("{\"foo\": \"bar\"}")) }
}
override lazy val app: Application = new GuiceApplicationBuilder()
//and override guice WSClient instance with MockWS
.overrides(bind[WSClient].toInstance(mockWs))
.build()
Siremock is a Wiremock wrapper. It's harder to set it up, but, it has many other features other than mock, for instance, verifying that your request was really made:
val request =
(on(urlEqualTo("/foo")))
.withHttpMethod(HttpMethods.POST)
.withContentType("application/json")
request respond (aResponse withBody "{\"foo\": \"bar\"}")
//execute your test here
verify(request) wasCalled exactly(1)
[]'s
Upvotes: 1