Libraries ScalaTest + Play ScalaTest + Selenium ScalaTest + ScalaCheck ScalaTest + JUnit 4 ScalaTest + JUnit 5 ScalaTest + TestNG ScalaTest + EasyMock ScalaTest + JMock ScalaTest + Mockito |
ScalaTest + JMockThe ScalaTest + JMock integration library makes it fun and easy to use JMock with ScalaTest. To use ScalaTest + JMock, please add the following to your SBT project dependency: libraryDependencies += "org.scalatestplus" %% "jmock-2-13" % "3.2.19.0" % "test" For maven you can use: <dependency> <groupId>org.scalatestplus</groupId> <artifactId>jmock-2-13_3</artifactId> <version>3.2.19.0</version> <scope>test</scope> </dependency>
Using the JMock API directly, you first need a val context = new Mockery
context.setImposteriser(ClassImposteriser.INSTANCE)
When using this class, you would instead create an instance of this class (which will create and
wrap a val cycle = new JMockCycle import cycle._ Using the JMock API directly, you would create a mock object like this: val mockCollaborator = context.mock(classOf[Collaborator]) Having imported the members of an instance of this class, you can shorten that to: val mockCollaborator = mock[Collaborator] After creating mocks, you set expectations on them, using syntax like this: context.checking( new Expectations() { oneOf (mockCollaborator).documentAdded("Document") exactly(3).of (mockCollaborator).documentChanged("Document") } ) Having imported the members of an instance of this class, you can shorten this step to: expecting { e => import e._ oneOf (mockCollaborator).documentAdded("Document") exactly(3).of (mockCollaborator).documentChanged("Document") }
The
The oneOf (mockCollaborator).documentAdded(`with`("Document"))
By importing the members of the passed
oneOf (mockCollaborator).documentAdded(withArg("Document"))
Once you've set expectations on the mock objects, when using the JMock API directly, you use the mock, then invoke
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)) context.assertIsSatisfied() This class enables you to use the following, more declarative syntax instead: whenExecuting { 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 cycle = new JMockCycle import cycle._
ScalaTest also provides a |
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.