Play Framework 2.5 服务器上的集成测试
Integration tests on a server on Play Framework 2.5
我正在 Play Framework 2.5 之上构建 REST 服务
我想创建一个集成测试来测试我的服务,就好像被外部客户端访问一样。
使用文档 here -- 我在测试中混合了 OneServerPerSuite class 并覆盖 'app' 所以:
implicit override lazy val app = new GuiceApplicationBuilder().build()
认为我不想覆盖我的路由器配置。
import org.scalatest.FreeSpec
import org.scalatestplus.play.OneServerPerSuite
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import scala.concurrent.Await
import scala.concurrent.duration.Duration
class ServerIntegration extends FreeSpec with OneServerPerSuite {
implicit override lazy val app = new GuiceApplicationBuilder().build()
"Server" - {
"When requested for a reply" - {
"should give it" in {
val ws = app.injector.instanceOf[WSClient]
val resonseFuture = ws.url(s"http://localhost:$port/my/defined/route").get()
val result = Await.result(resonseFuture, Duration.Inf)
println(result)
}}} }
我似乎成功启动了一个 HTTP 服务器,但它没有定义我的路由。我收到:'Action Not Found' 回复(状态 404)。
当 运行 服务器正常时 ('sbt run'),我可以访问我定义的所有操作(使用浏览器)。
您的代码完全正确。
如果放入项目中,它可以正常工作。
您可以通过从 play-scala
模板创建一个新项目来检查它,并将测试中的 URL 更改为 count
,例如
val resonseFuture = ws.url(s"http://localhost:$port/count").get()
结果是
[info] Server
[info] When requested for a reply
AhcWSResponse(200, OK)
[info] - should give it
[info] application - ApplicationTimer demo: Stopping application at 2016-12-04T19:44:03.761Z after 0s.
所以 - 再次检查 URL
找到我的错误。这很愚蠢,但可能对其他人有帮助:
在我的 conf/routes 文件中,我用 @ 定义了路由,如下所示:
GET / @com.company.service.controllers.serviceController.index
这导致路由是静态的,这就是它没有被注入的原因(在构建 GuiceApplicationBuilder 时)。
我正在 Play Framework 2.5 之上构建 REST 服务
我想创建一个集成测试来测试我的服务,就好像被外部客户端访问一样。
使用文档 here -- 我在测试中混合了 OneServerPerSuite class 并覆盖 'app' 所以:
implicit override lazy val app = new GuiceApplicationBuilder().build()
认为我不想覆盖我的路由器配置。
import org.scalatest.FreeSpec
import org.scalatestplus.play.OneServerPerSuite
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import scala.concurrent.Await
import scala.concurrent.duration.Duration
class ServerIntegration extends FreeSpec with OneServerPerSuite {
implicit override lazy val app = new GuiceApplicationBuilder().build()
"Server" - {
"When requested for a reply" - {
"should give it" in {
val ws = app.injector.instanceOf[WSClient]
val resonseFuture = ws.url(s"http://localhost:$port/my/defined/route").get()
val result = Await.result(resonseFuture, Duration.Inf)
println(result)
}}} }
我似乎成功启动了一个 HTTP 服务器,但它没有定义我的路由。我收到:'Action Not Found' 回复(状态 404)。
当 运行 服务器正常时 ('sbt run'),我可以访问我定义的所有操作(使用浏览器)。
您的代码完全正确。
如果放入项目中,它可以正常工作。
您可以通过从 play-scala
模板创建一个新项目来检查它,并将测试中的 URL 更改为 count
,例如
val resonseFuture = ws.url(s"http://localhost:$port/count").get()
结果是
[info] Server
[info] When requested for a reply
AhcWSResponse(200, OK)
[info] - should give it
[info] application - ApplicationTimer demo: Stopping application at 2016-12-04T19:44:03.761Z after 0s.
所以 - 再次检查 URL
找到我的错误。这很愚蠢,但可能对其他人有帮助:
在我的 conf/routes 文件中,我用 @ 定义了路由,如下所示:
GET / @com.company.service.controllers.serviceController.index
这导致路由是静态的,这就是它没有被注入的原因(在构建 GuiceApplicationBuilder 时)。