Libraries

ScalaTest + Play

ScalaTest + Selenium

ScalaTest + ScalaCheck

ScalaTest + JUnit

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-3-4" % "3.2.9.0" % "test"

For maven you can use:

<dependency>
    <groupId>org.scalatestplus</groupId>
    <artifactId>mockito-3-4_2.13</artifactId>
    <version>3.2.9.0</version>
    <scope>test</scope>
</dependency>    

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-2021 Artima, Inc. All Rights Reserved.

artima