Im following the play framework 2.5 documentation to write tests using web service clients
https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
The code below is extracted from above link which works. However as mentioned in the documentation, a random available port is assigned when using implicit port.
Is it possible to give a specific port instead of random port?
import play.core.server.Server
object GitHubClientSpec extends Specification with NoTimeConversions {
"GitHubClient" should {
"get all repositories" in {
Server.withRouter() {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" ->
"octocat/HelloWorld")))
}
} { implicit port =>
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
}
If you try to get into the definition of withRouter
, you'll see it takes ServerConfig
in which you can provide the port
and run mode
.
import play.core.server.Server
object GitHubClientSpec extends Specification with NoTimeConversions {
"GitHubClient" should {
"get all repositories" in {
//here 8888 is the port which you have defined.
Server.withRouter(ServerConfig(port = Some(8888), mode = Mode.Test)) {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" ->
"octocat/HelloWorld")))
}
} { implicit port =>
//The port will 8888 in this block
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
}
Hope that helps, happy coding :)