Meet Mockito

Learn more about mockito-scala, a mocking library for Scala.

What is mockito-scala?

In this lesson, we’ll take a look at one of the most common mocking libraries in Scala—mockito-scala. We can use mockito-scala with ScalaTest and Specs2. This course is about writing effective tests in Scala with ScalaTest, so we won’t focus on the integration with Specs2.

As you know, the first thing we do to import a new library is declare it in our Dependencies.scala file.

Press + to interact
Dependencies.scala
build.sbt
import sbt._
object Dependencies {
// Rest of the file as before
object scalatestplus {
val `mockito-4-6` = "org.scalatestplus" %% "mockito-4-6" % "3.2.15.0"
}
}

Mockito is a Java, not a Scala, library. So to use a more idiomatic syntax in Scala, a couple of wrappers have been developed over the years. One of them is mockito-scala, a project offering a convenient DSL encapsulating many, if not all, of the constructs provided by Mockito. However, according to its GitHub page at the time we published this course, mockito-scala is no longer actively maintained, and support for Scala 3 is missing.

For these reasons, we’ll use another wrapper of Mockito from ScalaTest Plus. ScalaTest Plus is a collection of integration libraries between ScalaTest and other testing libraries. As you can see from the declaration in Dependencies.scala ...