Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing Frontend integration with Play 2.x (Scala)

DZone's Guide to

Testing Frontend integration with Play 2.x (Scala)

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

For people in hurry here is the code and the steps.

In continuation of Play 2.x (Scala) is it a Spring MVC contender? – Introduction, in this blog, I will demonstrate how we implement a simple test to test the Web layer using TestServer. I will continue from my earlier example of Implementing Controller using Play 2.x (Scala), ScalaMock where I did the mock testing.

Refer Build.scala for build configuration.

In this blog, I will be testing the routes refer CoffeesControllerTest.scala for more details, the snippet is as below,

it("POST /coffees/Colombian/delete should return SEE_OTHER") {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
 
    val Some(result) = route(FakeRequest(POST, "/coffees/Colombian/delete"))
    status(result) should equal(SEE_OTHER)
  }
}
 
it("POST wrong url /coffees/Colombian/delete1 should return None") {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
 
    val result = route(FakeRequest(POST, "/coffees/Colombian/delete1"))
    result should equal(None)
  }
}

In the next block I will show how to do end to end testing from web layer using TestServer refer IntegrationTest.scala,

class IntegrationSpec extends Specification {
 
  "Application" should {
 
    "work from within a browser" in {
      running(TestServer(3333), HTMLUNIT) { browser =>
 
        browser.goTo("http://localhost:3333/coffees")
        browser.pageSource must contain("Colombian")
      }
    }
 
    "work from within a browser" in {
      running(TestServer(3333), HTMLUNIT) { browser =>
 
        browser.goTo("http://localhost:3333/coffees")
 
        browser.pageSource must contain("Colombian")
      }
    }
 
    "work from within a browser" in {
      running(TestServer(3333), HTMLUNIT) { browser =>
 
        browser.goTo("http://localhost:3333/coffees")
        browser.pageSource must not contain("Colombian1")
      }
    }
 
  }
}

Also while testing the application from web layer, we need to populate the data in the Global.scala as below,

object Global extends GlobalSettings {
 
  override def onStart(app: Application) {
 
    lazy val database = Database.forDataSource(DB.getDataSource())
 
    database withSession {
      // Create the tables, including primary and foreign keys
      val ddl = (Suppliers.ddl ++ Coffees.ddl)
 
      ddl.create
 
      // Insert some suppliers
      Suppliers.insertAll(
        Supplier(Some(101), "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
//...
      // Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
      Coffees.insertAll(
        Coffee(Some("Colombian"), 101, 799, 0, 0),
    }
  }
}

I hope this blog helped you. In the next blog I will implement simple authentication.


 

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}