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 = "https%3A%2F%2Fimage.shutterstock.com%2Fz%2Fstock-photo-le-bourget-near-paris-france-november-barack-obama-president-of-united-state-of-345821210.jpg"
        val baseUrl = s"http://127.0.0.1:19001/detect/url"
        val response = await(ws.url(baseUrl).withHttpHeaders(headers: _*).withQueryStringParameters("url" -> inputUrl).get(), 10, TimeUnit
          .SECONDS)

        response.status == 200
      }
    }
  }
}

But, I am getting the following error:

Connection refused: /127.0.0.1:19001
java.net.ConnectException: Connection refused: /127.0.0.1:19001

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 https://www.playframework.com/documentation/2.6.x/ScalaFunctionalTestingWithSpecs2#PlaySpecification

3 Likes

Hi Greg,

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

Lee

Good observation skills @greg. :slight_smile: