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



我正在用play和mockit在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已经存在。

谢谢!

也许这对某人会有帮助。

定义mockStuff时,应该反复实现相同的方法,但每次参数都不同,然后在调用该方法时,提供参数指向您在函数中编写的实现。
另一个应该记住的问题是,应该只抛出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
    }

相关内容

  • 没有找到相关文章

最新更新