使用 ScalaTest 编写测试 class 时无法在 Scala 中模拟 class

Unable to mock class in scala while writing test class with ScalaTest

我正在使用 scala 2.11 和 scalatest 2.11。 我正在尝试模拟一个 class 用于单元测试 class.

Vector class 有一个方法“vectorSum”,它添加 2 个向量和 returns 结果向量。

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 class 有一个方法“findMaxVectorSum”,它接受一个向量数组和 returns 对具有最高总和的数组索引。 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 class 并测试 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.apply(VectorUtilTest.scala:26)
Actually, there were zero interactions with this mock.

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

非常感谢您。

如您的错误所述,这里的问题是未调用 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])
}

此外,您可能想阅读 什么时候可以在 Scala 中使用“var”? Return in Scala