Libraries

ScalaTest + Play

ScalaTest + Selenium

ScalaTest + ScalaCheck

ScalaTest + JUnit 4

ScalaTest + JUnit 5

ScalaTest + TestNG

ScalaTest + EasyMock

ScalaTest + JMock

ScalaTest + Mockito

ScalaTest + Mockito

The ScalaTest + Mockito integration library makes it fun and easy to use Mockito with ScalaTest. To use ScalaTest + Mockito, please add the following to your SBT project dependency:

libraryDependencies += "org.scalatestplus" %% "mockito-5-10" % "3.2.18.0" % "test"

For maven you can use:

<dependency>
    <groupId>org.scalatestplus</groupId>
    <artifactId>mockito-5-10_3</artifactId>
    <version>3.2.18.0</version>
    <scope>test</scope>
</dependency>    

Mockito 5 requires JDK 11, if you are on JDK 8, you'll need to use mockito-4-11 instead.

MockitoSugar trait provides some basic syntax sugar for Mockito.

Using the Mockito API directly, you create a mock with:

val mockCollaborator = mock(classOf[Collaborator])

Using this trait, you can shorten that to:

val mockCollaborator = mock[Collaborator]

Here is how the example used in the previous EasyMock and JMock sections would look with Mockito and MockitoSugar:

// First, create the mock object
val mockCollaborator = mock[Collaborator]

// Create the class under test and pass the mock to it
classUnderTest = new ClassUnderTest
classUnderTest.addListener(mockCollaborator)

// Use the class under test
classUnderTest.addDocument("Document", new Array[Byte](0))
classUnderTest.addDocument("Document", new Array[Byte](0))
classUnderTest.addDocument("Document", new Array[Byte](0))
classUnderTest.addDocument("Document", new Array[Byte](0))

// Then verify the class under test used the mock object as expected
verify(mockCollaborator).documentAdded("Document")
verify(mockCollaborator, times(3)).documentChanged("Document")

ScalaTest is brought to you by Bill Venners and Artima.
ScalaTest is free, open-source software released under the Apache 2.0 license.

If your company loves ScalaTest, please consider sponsoring the project.

Copyright © 2009-2024 Artima, Inc. All Rights Reserved.

artima