Implementing Controller using Play 2.x (Scala), ScalaMock

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 Controller implementation using ScalaTest / ScalaMock. I will continue from my earlier example of Implementing DAL in Play 2.x (Scala), Slick, ScalaTest of the basic CRUD operations on Coffee catalogue.

In this example as well we need the ScalaMock dependency in Build.scala as below,

"com.typesafe.slick" %% "slick" % "1.0.0",
"org.scalamock" %% "scalamock-scalatest-support" % "3.0" % "test",

In our Previous example, we have already implemented a DAL trait called CoffeeComponent with basic CRUD as below,

trait CoffeeComponent {
  def find(pk: String): Query[Coffees.type, Coffee]
  def findall(pk: String): Query[(Coffees.type, Suppliers.type), (Coffee, Supplier)]
  def list(page: Int, pageSize: Int, orderBy: Int, filter: String): Query[(Coffees.type, Suppliers.type), (Coffee, Supplier)]
  def delete(pk: String): Int

We are construction injecting CoffeeComponent to the CoffeeController and create a Singleton object with the same name, as below,

class CoffeesController(coffeeComponent: CoffeeComponent) extends Controller {
  def delete(pk: String) = Action {
    database withSession {
      println("in db session")
      Home.flashing(coffeeComponent.delete(pk) match {
        case 0 => "failure" -> "Entity has Not been deleted"
        case x => "success" -> s"Entity has been deleted (deleted $x row(s))"

object CoffeesController extends CoffeesController(new CoffeeComponentImpl)

As seen above, we have a delete method, we will build a ScalaMock to mock the delete method of coffeeComponent and control the expected behavior to return 1 row effected and assert for HTTP SEE_OTHER status as below,

class CoffeesControllerTest extends FunSpec with ShouldMatchers with MockFactory {
  describe("Coffee Controller with Mock test") {

    it("should delete a coffee record with assert on status") {
      running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
        val mockComponent = mock[CoffeeComponent]
        (mockComponent.delete _).expects("Columbian") returning (1) twice
        val controller = new CoffeesController(mockComponent)
        mockComponent.delete("Columbian") should equal (1)
        val result = controller.delete("Columbian")(FakeRequest())
        status(result) should equal (SEE_OTHER)

If you notice, we are extending FunSpec of ScalaTest for BDD. Also the HTTP status is SEE_OTHER, this is because the Success is redirected to Index page.

Now if you run the ScalaTest you will see the result in STS as below,

ScalaTest Coffee Example with ScalaMock

ScalaTest Coffee Example with ScalaMock

I hope this blog helped. In my next blog, I will talk about controller routes testing and Frontend testing.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s