Search code examples
scalaunit-testingmockitoscalatest

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


I am using scala 2.11 with scalatest 2.11. I am trying to mock a class for unit test a class.

Vector class has a method "vectorSum" that adds 2 vectors and returns the resulting vector.

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 has a method "findMaxVectorSum" that takes an Array of vectors and returns pair of array indexes that has highest sum. 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)
  }
}

In VectorUtilTest, I am trying to mock the Vector class and test the findMaxVectorSum method.

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])
  }
}

But when I run this test method, I get output as follows:

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.

I have wasted way too much time on this and I am very frustrated now. Can anybody please assist me with this?

Thank you very much in advance.


Solution

  • The issue here, as described in your error, is that mockVector.vectorSum was not called. When calling:

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

    Mockito expects to find a call on the mocked instance. As we can see in your test code, the mocked vector did not go to the findMaxVectorSum, hence did not call vectorSum. I am not sure what exactly you are trying to test here, but maybe you should add the mocked vector into the array.

    For example, a passing test will be:

    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])
    }
    

    In addition, you'd probably want to read When is it okay to use “var” in Scala? and Return in Scala