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

问题描述

我正在将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)
    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.

我在此上浪费了太多时间,现在我感到非常沮丧。 有人可以帮我吗?

非常感谢您。

解决方法

如您的错误所述,这里的问题是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