Play + Test using withServer getting connection error

Hi everyone,

I am new to play framework. I followed the example on ScalaFunctionalTestingWithSpecs2 to create the following functional test for my application:

class HomeControllerTest extends PlaySpec with FutureAwaits with DefaultAwaitTimeout {

  "lookupByUrl" should {
    "return result" in new WithServer {
      WsTestClient.withClient { ws =>
        val headers = Seq("Accept" -> "application/json")
        val inputUrl = ""
        val baseUrl = s""
        val response = await(ws.url(baseUrl).withHttpHeaders(headers: _*).withQueryStringParameters("url" -> inputUrl).get(), 10, TimeUnit

        response.status == 200

But, I am getting the following error:

Connection refused: / Connection refused: /

I have been stuck on this for a while. Any help is appreciated!

Thank you!

Hmm, I wonder if the local server’s not listening on that port? Can you put a long Thread.sleep() inside your test code then (a) run netstat to check what port it’s listening on and (b) try curl or telnet to test the connection.

Hi Rich,

Thank you for your reply and suggestions. Before I start the suggested troubleshooting, I would like to clarify that I didn’t have a separate application running on a server. According to the ScalaFunctionalTestingWithSpecs2, a server will be started automatically and running on default port 19001. If a separate instance is started, then the test will be able to connect without any problems.

I would like to run functional test without manually starting a separate application instance on a server. I don’t how other experienced play developers write functional tests. Any suggestions or help is appreciated!

I noticed you’re using PlaySpec which is from scalatest+play. I think you meant to use play.api.test.PlaySpecification from play-specs2, since WithServer is a specs2 scope. See


Hi Greg,

That is it! I was using the wrong test framework. Thank you so much for spotting the problem!


Good observation skills @greg. :slight_smile: