6

I would like to know what's the best way to run specs2 tests on a PlayFramework module and be able to simulate it running.

My module contains some routes in a file named mymodule.routes In my apps I integrate them by adding the following line in my routes file

->  /mymodule mymodule.Routes

This is the test from my module I try to run but returns a 404 error :

"test myroute" in {
  running(FakeApplication()) {
    await(WS.url("http://localhost:9000/mymodule/myroute").get).status must equalTo(OK)
  }
}
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Roch
  • 21,741
  • 29
  • 77
  • 120

1 Answers1

4

FakeApplication does not really lunch a web process, so you cannot test using http access to localhost.

You have three options:

  • Testing the controller directly
  • Testing the router
  • Testing the whole app.

Testing the controller is done by directly calling your controller and checking the result, as suggested in play documentation, and providing a FakeRequest()

val result = controllers.Application.index("Bob")(FakeRequest())

Testing the router is done by calling routeAndCall with a FakeRequest argument, specifying the relative path:

val Some(result) = routeAndCall(FakeRequest(GET, "/Bob"))

Eventually, if you want to test your whole application, you need to start a TestServer:

"run in a server" in {
  running(TestServer(3333)) {

    await(WS.url("http://localhost:3333").get).status must equalTo(OK)

  }
}

Your question says: "What is the best option?". The answer is: there is nothing such as a best option, there are different way of testing for different purpose. You should pick up the testing strategy which better fits your requirement. In this case, since you want to test the router, I suggest you try approach n.2

Edmondo
  • 19,559
  • 13
  • 62
  • 115
  • thanks it makes sense, however I don't know what to do for the routes because they return 404 unless they are implemented by a parent application. I have tried to add a basic route file in the module just for the test but it doesn't work unless I delete mymodule.routes – Roch May 16 '13 at 12:24
  • You cannot test the url unless you start a TestServer. Read again my answer – Edmondo May 16 '13 at 14:53