Search code examples
scalaasynchronousmockitoscalatestmockito-scala

How to use MockitoSugar with AsyncFunSuite in Scalatest?


I'm not able to use MockitoSugar along with AsyncFunSuite. In short:

This example works (taken from Scalatest documentation)

class AddSuite extends AsyncFunSuite {

  def addSoon(addends: Int*): Future[Int] = Future { addends.sum }

  test("addSoon will eventually compute a sum of passed Ints") {
    val futureSum: Future[Int] = addSoon(1, 2)
    futureSum map { sum => assert(sum == 3) }
  }

}

However this example doesn't (because it has MockitoSugar)

class AddSuite extends AsyncFunSuite with MockitoSugar {

  def addSoon(addends: Int*): Future[Int] = Future { addends.sum }

  test("addSoon will eventually compute a sum of passed Ints") {
    val futureSum: Future[Int] = addSoon(1, 2)
    futureSum map { sum => assert(sum == 3) }
  }

}

Both compiles but zero tests were reported for the latter while one test was correctly reported for the former. This happens on both IntelliJ and sbt. Why doesn't this work? And how can I fix this?

I'm using:

  • scala 2.12
  • scalatest 3.1.1
  • mockito-core 3.3.3
  • mockito-scala 1.13.10

Solution

  • Nevermind, figured out after looking through the mockito-scala codebase. I was supposed to use AsyncMockitoSugar as opposed to MockitoSugar. So it should've been:

    class AddSuite extends AsyncFunSuite with AsyncMockitoSugar {
    
      def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
    
      test("addSoon will eventually compute a sum of passed Ints") {
        val futureSum: Future[Int] = addSoon(1, 2)
        futureSum map { sum => assert(sum == 3) }
      }
    
    }