0
votes

Pretty new to scala and play and I have been assigned a task to test someone else app,which is running fine btw.Please check if my tests are right and what is the error.

This is employeeEntry.scala file in models

 package models

import models.database.Employee
import play.api.db.slick.Config.driver.simple._
import play.api.db.slick._
import play.api.Play.current


case class EmployeeEntry(eid :Int, ename: String, eadd: String, emob: String)

object Employee {

val DBemp = TableQuery[Employee]


def savedat(value: EmployeeEntry):Long = {
    DB.withSession { implicit session =>
       DBemp+=EmployeeEntry(eid=value.eid,ename=value.ename,eadd=value.eadd,emob=value.emob)
    }}

/*val query = for (c <- Employee) yield c.ename 
    val result = DB.withSession {
                                 implicit session =>
                                 query.list // <- takes session implicitly
                                }*/
        //val query = for (c <- Employee) yield c.ename
   def getPersonList: List[EmployeeEntry] = DB.withSession { implicit session =>      DBemp.list }

  def Update: Int = DB.withSession { implicit session =>
 (DBemp filter (_.eid === 1) map (s => (s.ename,s.eadd))) update ("test","khair")}

  def delet :Int =DB.withSession {
  implicit session => (DBemp filter (_.eid === 1)).delete
}






 }

And this is file Employee.scala in models/database

 package models.database

 import models._
 import models.EmployeeEntry
 import play.api.db.slick.Config.driver.simple._
 import scala.slick.lifted._

 class Employee(tag:Tag) extends Table[EmployeeEntry](tag,"employee") 
 {



   //val a = "hello"
  def eid = column[Int]("eid", O.PrimaryKey)
  def ename = column[String]("name", O.DBType("VARCHAR(50)"))
  def emob = column[String]("emob",O.DBType("VARCHAR(10)"))
  def eadd =column[String]("eadd",O.DBType("VARCHAR(100)"))
  def * = (eid, ename, emob, eadd) <> (EmployeeEntry.tupled, EmployeeEntry.unapply)


 }

Finally this is my test I am running,which is failing :

 import play.api.db.slick.Config.driver.simple._
 import play.api.db.slick._
 import play.api.Play.current



 import org.scalatest.FunSpec
 import org.scalatest.matchers.ShouldMatchers
 import models.database.Employee
 import scala.slick.lifted._ 
 import models._
 import models.EmployeeEntry
 //import scala.slick.driver.H2Driver.simple._


 class databasetest extends FunSpec with ShouldMatchers{


   describe("this is to check database layer"){ 

       it("can save a row"){

            val a = EmployeeEntry(1006,"udit","schd","90909090")
            Employee.savedat(a) should be (1)


      }     


     it("getpersonlist"){



            Employee.getPersonList.size should be (1)


    }


     }

  }

The test is failing and error is

  java.lang.RuntimeException: There is no started application
  at scala.sys.package$.error(package.scala:27)
  at play.api.Play$$anonfun$current$1.apply(Play.scala:71)
  at play.api.Play$$anonfun$current$1.apply(Play.scala:71)
  at scala.Option.getOrElse(Option.scala:120)
  at play.api.Play$.current(Play.scala:71)
  at models.Employee$.getPersonList(EmployeeEntry.scala:27)
  at databasetest$$anonfun$1$$anonfun$apply$mcV$sp$2.apply$mcV$sp(databasetest.scala:39)
  at databasetest$$anonfun$1$$anonfun$apply$mcV$sp$2.apply(databasetest.scala:39)
  at databasetest$$anonfun$1$$anonfun$apply$mcV$sp$2.apply(databasetest.scala:39)
  at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
1

1 Answers

0
votes

By default play provides spec2 testing framework.so no need to add scalatest framework for unit testing.For database access layer testing required a connection with database so start a fake application.(this is not running code, just an idea to write unit test) for more detail take a look play doc : https://www.playframework.com/documentation/2.3.x/ScalaFunctionalTestingWithSpecs2

import org.specs2.mutable.Specification

import models.database.Employee
import models._
import models.EmployeeEntry
import play.api.test.FakeApplication
import play.api.test.Helpers.running
import play.api.Play.current


class databasetest extends Specification {

 "database layer" should {
     "save a row" in {
        running(FakeApplication()) {
           val a = EmployeeEntry(1006,"udit","schd","90909090")
           Employee.savedat(a) must be equalTo (1)
         }
       }
    "get list" in {
         running(FakeApplication()) {
           Employee.getPersonList.size must be equalTo (1)
         }
     }
}