Now we have all that we need to create a generic mock implementation and mock all the dao
packages. Under the location ReactiveWebStore/test/
, we will create a package called mocks, and create a call, GenericMockedDao
.
Your GenericMockedDao.scala
file should look like this:
package mocks import models.BaseModel class GenericMockedDao[T <: BaseModel] { import java.util.concurrent.atomic.AtomicLong import scala.collection.mutable.HashMap import scala.concurrent._ import play.api.libs.concurrent.Execution. Implicits.defaultContext var inMemoryDB = new HashMap[Long,T] var idCounter = new AtomicLong(0) def findAll(): Future[Seq[T]] = { Future { if (inMemoryDB.isEmpty) Seq() inMemoryDB.values.toSeq } } def findById(id:Long): Future[Option[T]] = { Future { inMemoryDB.get(id) } } def remove(id:Long): Future[Int] = { Future { validateId(id) inMemoryDB.remove(id) 1 } } ...