我正在Play Framework 2.5上构建REST服务
我想创建一个集成测试来测试我的服务,就像访问外部客户端一样。
使用文档here - 我在测试类中混合了OneServerPerSuite并覆盖'app',所以:
隐式覆盖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服务器,但没有定义我的路由。我收到:'未找到行动'回复(状态404)。
正常运行服务器('sbt run')时,我可以访问所有已定义的操作(使用浏览器)。
答案 0 :(得分:0)
您的代码完全正确。
如果放入项目,它的工作原理。
您可以通过play-scala
模板创建新项目来检查它,并将测试中的网址更改为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.
所以 - 再次检查网址
答案 1 :(得分:0)
发现我的错误。这很愚蠢但可能对其他人有帮助:
在我的conf / routes文件中,我用@定义了路由,如下所示:
GET / @com.company.service.controllers.serviceController.index
这导致路由是静态的,这就是它未被注入的原因(构建GuiceApplicationBuilder时)。