Testing routes
The Play framework allows us to test routes as well. This is good, because as our application grows and we refactor the code, we can be 100% sure that our routes are functioning. Route testing could be easily confused with controller testing. The main difference is that, with routing testing, we should test if we are able to reach the routes and that's it. After route testing, we will cover controller testing in detail.
RoutesTestingSpec.scala - Play framework route testing
Your RoutesTestingSpec.scala file should contain the following code:
class RoutesTestingSpec extends PlaySpec with OneAppPerTest {
"Root Controller" should {
"route to index page" in {
val result = route(app, FakeRequest(GET, "/")).get status(result) mustBe OK
contentType(result) mustBe Some("text/html")
contentAsString(result) must include ("Welcome to Reactive Web Store")
}
}
"Product Controller" should {
"route to index page" in {
val result = route(app, FakeRequest(GET, "/product")).get status(result) mustBe OK
contentType(result) mustBe Some("text/html") contentAsString(result) must include ("Product")
}
"route to new product page" in {
val result = route(app, FakeRequest(GET,
"/product/add")).get status(result) mustBe OK
contentType(result) mustBe Some("text/html") contentAsString(result) must include ("Product")
}
"route to product 1 details page page" in { try{
route(app, FakeRequest(GET, "/product/details/1")).get }catch{
case e:Exception => Unit
}
}
}
"Review Controller" should {
"route to index page" in {
val result = route(app, FakeRequest(GET, "/review")).get status(result) mustBe OK
contentType(result) mustBe Some("text/html") contentAsString(result) must include ("Review")
}
"route to new review page" in {
val result = route(app, FakeRequest(GET,
"/review/add")).get
status(result) mustBe OK
contentType(result) mustBe Some("text/html") contentAsString(result) must include ("review")
}
"route to review 1 details page page" in { try{
route(app, FakeRequest(GET, "/review/details/1")).get }catch{
case e:Exception => Unit
}
}
}
"Image Controller" should {
"route to index page" in {
val result = route(app, FakeRequest(GET, "/image")).get status(result) mustBe OK
contentType(result) mustBe Some("text/html") contentAsString(result) must include ("Image")
}
"route to new image page" in {
val result = route(app, FakeRequest (GET, "/image/add")).get status(result) mustBe OK
contentType(result) mustBe Some("text/html") contentAsString(result) must include ("image")
}
"route to image 1 details page page" in { try{
route(app, FakeRequest(GET, "/image/details/1")).get }catch{
case e:Exception => Unit
}
}
}
}
So here we have tests for all our main controllers, which are root, product, review, and image. RootController is the controller of the main page when you visit http ://localho st :9000.
There is a special helper function called route in the Play framework, which helps us to test routes. Then we use FakeRequest pass the path to the route. It's possible to test the status code and content type of the page to which the router routed our request.
For product, image, and review controllers, you can see we are trying to call an item that does not exist. That's why we have the try... catch, because we expect to have an exception happening there.
$ activator "test-only RoutesTestingSpec"
Executing this preceding command produces the following result:
