

Test Case Of Data Access Layer Function in Play:-
source link: https://blog.knoldus.com/test-case-of-data-access-layer-function-in-play/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Test Case Of Data Access Layer Function in Play:-
Reading Time: < 1 minute
In this blog I am trying to show how to create test case of data access layer function.
Lets have following function of DAL (data access layer) as an example in models packegae of play.
All the necessary files must be imported to avoid errors.
xxxxxxxxxx
package model.dals
import scala.slick.driver.PostgresDriver.simple._
import scala.slick.session.Session
import model.domains.Domain._
import play.api.Logger
import utils.Connection
trait MobileDALComponent {
def getMobileRecordByIMEID(imeid: String): List[Mobile]
}
class MobileDAL extends MobileDALComponent {
override def getMobileRecordByIMEID(imeid: String): List[Mobile] = {
Connection.databaseObject().withSession { implicit session: Session =>
Logger.info("Calling getMobileRecordByIMEID" + imeid)
(for { mobile <- Mobiles if ((mobile.imeiMeid === imeid) || (mobile.otherImeiMeid=== imeid)) } yield mobile).list
}
}
object MobileDAL extends MobileDAL
Now the test case of above function must be created in Test directory in play.
In this test case I am using FunSuite test library :- A suite of tests in which each test is represented as a function value. The “Fun” in FunSuite stands for “function.”
All the necessary files must be imported.
The values passed to the DAL function in a test is dummy data.Once the DAL function is processed the expected result is asserted.
xxxxxxxxxx
package model.dals
import org.scalatest.FunSuite
import model.domains.Domain._
import model.dals.MobileDAL._
import play.api.test.FakeApplication
import play.api.test.Helpers._
import play.api.Logger
import org.scalatest.BeforeAndAfter
class mobileDALTest extends FunSuite{
val date = new java.sql.Date(new java.util.Date().getTime())
val brand=Brand("nokia",date)
val model=MobileModels("N72",1)
val mobileUser = Mobile(
"test", 1, 5, "12345678901234", "12345678902134", "12-05-2013", "+91 1111111111",
"[email protected]","stolen",Status.pending, "ddas asd","10-10-2013","test.png","Sigma","Sigma454")
test("DBLayerTesting: Get User by IMEID number successfully ") {
running(FakeApplication()) {
val user = MobileDAL.getMobileRecordByIMEID("12345678901234")
assert(user.head.email === "[email protected]")
}
}
If the asserted value is correct the test passes.
reference*
http://www.artima.com/docs-scalatest-2.0/index.html#org.scalatest.FunSuite
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK