|
Libraries ScalaTest + Play ScalaTest + Selenium ScalaTest + ScalaCheck ScalaTest + JUnit 4 ScalaTest + JUnit 5 ScalaTest + TestNG ScalaTest + EasyMock ScalaTest + JMock ScalaTest + Mockito |
ScalaTest + EasyMockThe ScalaTest + EasyMock integration library makes it fun and easy to use EasyMock with ScalaTest. To use ScalaTest + EasyMock, please add the following to your SBT project dependency: libraryDependencies += "org.scalatestplus" %% "easymock-5-3" % "3.2.19.0" % "test" For maven you can use:
<dependency>
<groupId>org.scalatestplus</groupId>
<artifactId>easymock-5-3_3</artifactId>
<version>3.2.19.0</version>
<scope>test</scope>
</dependency>
ScalaTest's Using the EasyMock API directly, you create a mock with: val mockCollaborator = createMock(classOf[Collaborator]) With this trait, you can shorten that to: val mockCollaborator = mock[Collaborator] After creating mocks, you set expectations on them, using syntax like this: mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") expectLastCall().times(3)
If you wish to highlight which statements are setting expectations on the mock (versus
which ones are actually using the mock), you can place them in an
expecting {
mockCollaborator.documentAdded("Document")
mockCollaborator.documentChanged("Document")
lastCall.times(3)
}
Using an
Once you've set expectations on the mock objects, you must invoke replay(mockCollaborator) 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)) verify(mockCollaborator) This trait enables you to use the following, more declarative syntax instead:
whenExecuting(mockCollaborator) {
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))
}
The
To summarize, here's what a typical test using val mockCollaborator = mock[Collaborator]
An alternative approach is to place your mock objects in a implicit val mocks = MockObjects(mock[Collaborator]) |
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-2025 Artima, Inc. All Rights Reserved.