DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Implementing Controller using Play 2.x (Scala), ScalaMock

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

Krishna Prasad user avatar by
Krishna Prasad
·
Apr. 03, 13 · Interview
Like (0)
Save
Tweet
Share
3.80K Views

Join the DZone community and get the full member experience.

Join For 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 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 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.


Blog Build (game engine) Spring Framework Trait (computer programming) Row (database) Implementation Object (computer science)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Build an Effective CI/CD Pipeline
  • Practical Example of Using CSS Layer
  • 10 Easy Steps To Start Using Git and GitHub
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: