在scala / mockito / play中的同一类中的测试之间替换mockito函数

时间:2014-03-09 15:49:10

标签: scala junit mockito playframework-2.1

我正在使用play和mockito对我的scala进行测试 这是我的代码:

@RunWith(classOf[JUnitRunner])
class ProductServiceTests extends Specification
with ProductRepositoryComponent
with ProductServiceComponentImpl
with Mockito
 {

  val productRepository = mock[ProductRepository]
  val productId = "d3d08285-512f-46a6-811f-1abeb94ebb98"
  val product:Product = new Product(Option(productId), "default name", "default description", new References(Some("1"), Some("1"), Some("1"), Some("1")))
  val language = "en_US"
  val tenantId = ""

  def mockStuff = {
    productRepository.addProduct(any[String], any[String], any[Product]) returns
    product.id.get

    productRepository.updateProduct(any[String], any[String], any[Product]) returns
    product.id.get
  }

  step(mockStuff)

  "ProductService" should {
    "add minimal product to product repository" in {
      val result = productService.addProduct(language, tenantId, product)
      result mustNotEqual null
      result must beAnInstanceOf[DTOResponse[String]]
      val resultAsStr = result.asInstanceOf[DTOResponse[String]].get
      resultAsStr.length mustEqual 36 //Guid length
      resultAsStr mustEqual productId
    }

     //How can i override addProduct - So that from now on, it will use the exception throwing add Product.. (commented one)
    "update product in repository" in {
      val result = productService.addProduct("he_IL", tenantId, product)
      result mustNotEqual null
      result must beAnInstanceOf[DTOResponse[String]]
      val resultAsStr = result.asInstanceOf[DTOResponse[String]].get
      resultAsStr.length mustEqual 36 //Guid length
      resultAsStr mustEqual productId
    }
  }  
}    

我在2里面有 ..如何覆盖第二个的addProduct方法?

我的问题是我要模拟2个addProduct函数,一个是好的,另一个将无效,因为Id已经存在..

谢谢!

1 个答案:

答案 0 :(得分:0)

也许这会对某人有所帮助..

在定义mockStuff时,One应该反复实现相同的方法,但每次参数都不同,然后在调用该方法时,将params点提供给您在函数中编写的实现。
应该记住的另一个问题是只应抛出 RuntimeException 。  例如,在我的样本中,当我把He_IL放入时我希望抛出异常,所以我用字符串" He_IL"来调用我的函数。然后我会得到" getProduct"抛出异常:

with Mockito
 {
  val productRepository = mock[ProductRepository]
  val productId = "d3d08285-512f-46a6-811f-1abeb94ebb98"
  val product:Product = new Product(Option(productId), "default name", "default description", new References(Some("1"), Some("1"), Some("1"), Some("1")))
  val language = "en_US"
  val tenantId = ""

  def mockStuff = {
    productRepository.addProduct(any[AddProductRequest]) returns
    product.id.get

    productRepository.addProduct(AddProductRequest("He_IL",tenantId, product)) throws new RuntimeException("Language must be english !! !!")        
  }

step(mockStuff)

  "ProductService" should {
    "add product with only required fields to product repository" in {
      val result = productService.addProduct(AddProductRequest(language, tenantId, product))
      result mustNotEqual null
      result must beAnInstanceOf[DTOResponse[String]]
      val resultAsStr = result.asInstanceOf[DTOResponse[String]].get
     resultAsStr mustEqual productId
    }


    "add product when getting exception from product repository" in {
      val result = productService.addProduct(AddProductRequest("He_IL", tenantId, product))
      result mustNotEqual null
      result must beAnInstanceOf[DTOResponse[String]]
      val resultAsStr = result.asInstanceOf[DTOResponse[String]].get
     resultAsStr mustEqual productId
    }