Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Building Applications with Scala
Source

Refactoring tests

As you might expect, most tests are no longer working. We will need to perform some refactoring here as well. We will refactor our former dao to make it generic, and it will be used in integration tests (end-to-end tests).

Since we will create a generic dao system in memory for end-to-end testing purposes, we need to change our models a little bit. First, we need to create a base trait for all the models. This is needed so we can treat our models as equals.

Let's have a look at models.BaseModel.scala:

package models trait BaseModel {

def getId:Option[Long]

def setId(id:Option[Long]):Unit

}

We also need to make all our models implement this new trait. So we will need to change the Scala code for the product, image, and review. This is very trivial: we just add a getter and a setter for the id field. You can also use scala.bean.BeanProperty instead of writing one by yourself.

Your models.Product.scala file should look something like this:

package models case class Product ( var id:Option[Long], var name:String, var details:String, var price:BigDecimal ) extends BaseModel{

override def toString:String = {

"Product { id: " + id.getOrElse(0) + ",name: " + name + ", details: "+ details + ", price: " + price + "}"

}

override def getId:Option[Long] = id

override def setId(id:Option[Long]):Unit = this.id = id

}

object ProductDef{

def toTable:String = "Product"

}

As you can see in the preceding code, we extend the BaseModel method, and implement getld and setld. We need to do the same for the review and image models. Your models.Review.scala file should look like this:

package models case class Review (var id:Option[Long],

var productId:Option[Long], var author:String, var comment:String) extends BaseModel{

override def toString:String = {

"Review { id: " + id + " ,productId: " +

productId.getOrElse(0) + ",author: " + author + ",comment: "

+ comment + " }"

}

override def getId:Option[Long] = id

override def setId(id:Option[Long]):Unit = this.id = id

}

object ReviewDef{

def toTable:String = "Review"

}

Now we move on to the last model. We need to implement it in Image.scala. Your models.Image.scala file should look like this:

package models case class Image (var id:Option[Long],

var productId:Option[Long], var url:String) extends BaseModel {

override def toString:String = {

"Image { productId: " + productId.getOrElse(0) + ",url: " + url + "}"

}

override def getId:Option[Long] = id

override def setId(id:Option[Long]):Unit = this.id = id

}

object ImageDef{

def toTable:String = "Image"

}

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel