【问题标题】:Unable to mock class in scala while writing test class with ScalaTest使用 ScalaTest 编写测试类时无法在 scala 中模拟类
【发布时间】:2021-01-07 00:27:18
【问题描述】:

我正在使用 scala 2.11 和 scalatest 2.11。 我正在尝试模拟一个类以对一个类进行单元测试。

Vector 类有一个方法“vectorSum”,可以将 2 个向量相加并返回结果向量。

Vector.scala

package com.unitTestDemo

class Vector(d_x:Int,d_y:Int,d_z:Int) {
  var x = d_x
  var y = d_y
  var z = d_z

  def vectorSum(second:Vector): Vector = {
    var result = new Vector(0,0,0)
    result.x = x + second.x
    result.y = y + second.y
    result.z = z + second.z

    return result
  }
}

VectorUtil 类有一个方法“findMaxVectorSum”,它接受一个向量数组并返回具有最高和的数组索引对。 VectorUtil.scala

package com.unitTestDemo


class VectorUtil {

  def findMaxVectorSum(vectorArray:Array[Vector]): Unit ={

    var max = 0.0
    var returnI = 0
    var returnj = 0

    for(i <- 0 to vectorArray.length-2){
      for(j <- i+1 to vectorArray.length-1){
        var temp = vectorArray(i)vectorSum(vectorArray(j))
        var tempMax = math.sqrt(temp.x*temp.x + temp.y*temp.y + temp.z*temp.z)
        if(tempMax > max) {
          max = tempMax
          returnI = i
          returnj = j
        }
      }
    }

    return (returnI,returnj)
  }
}

在 VectorUtilTest 中,我试图模拟 Vector 类并测试 findMaxVectorSum 方法。

VectorUtilTest.scala

package com.unitTestDemo

import org.mockito.ArgumentMatchers._
import org.mockito.Mockito
import org.mockito.Mockito.when
import org.mockito.MockitoSugar.verify
import org.scalatest.{FunSuite, Matchers}
import org.scalatest.mockito.MockitoSugar

class VectorUtilTest extends FunSuite with MockitoSugar with Matchers{

  test("testFindMaxVectorSum") {

    val vectorArray:Array[Vector] = new Array[Vector](3)

    vectorArray(0) = new Vector(1,2,3)
    vectorArray(1) = new Vector(2,3,4)
    vectorArray(2) = new Vector(3,4,5)

    val temp = new Vector(1,1,1)
    val mockVector = mock[Vector]
    when(mockVector.vectorSum(any[Vector])).thenReturn(temp)

    val vectorUtil = new VectorUtil()
    vectorUtil.findMaxVectorSum(vectorArray)

    verify(mockVector,Mockito.atLeastOnce).vectorSum(any[Vector])
  }
}

但是当我运行这个测试方法时,我得到如下输出:

Wanted but not invoked:
vector.vectorSum(<any>);
-> at com.unitTestDemo.VectorUtilTest$$anonfun$1.apply(VectorUtilTest.scala:26)
Actually, there were zero interactions with this mock.

我在这方面浪费了太多时间,现在我很沮丧。 有人可以帮我解决这个问题吗?

非常感谢您。

【问题讨论】:

    标签: scala unit-testing mockito scalatest


    【解决方案1】:

    如您的错误中所述,这里的问题是 mockVector.vectorSum 未被调用。调用时:

    verify(mockVector,Mockito.atLeastOnce).vectorSum(any[Vector])
    

    Mockito 期望在模拟实例上找到调用。正如我们在您的测试代码中看到的,模拟向量没有转到findMaxVectorSum,因此没有调用vectorSum。我不确定你究竟想在这里测试什么,但也许你应该将模拟向量添加到数组中。

    例如,通过测试将是:

    test("testFindMaxVectorSum") {
    
      val temp = new Vector(1,1,1)
      val mockVector = mock[Vector]
      when(mockVector.vectorSum(any[Vector])).thenReturn(temp)
      
      val vectorArray:Array[Vector] = new Array[Vector](4)
    
      vectorArray(0) = mockVector
      vectorArray(1) = new Vector(1,2,3)
      vectorArray(2) = new Vector(2,3,4)
      vectorArray(3) = new Vector(3,4,5)
    
      val vectorUtil = new VectorUtil()
      vectorUtil.findMaxVectorSum(vectorArray)
    
      verify(mockVector,Mockito.atLeastOnce).vectorSum(any[Vector])
    }
    

    此外,您可能还想阅读When is it okay to use “var” in Scala? Return in Scala

    【讨论】:

    • 它的工作就像一个魅力....感谢一吨人!我只是想测试模拟功能,仅此而已。但这对我来说成了一场噩梦。我希望我能给你一个款待。上帝保佑你:)
    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多