ScalaMock is native Scala mocking framework.
Mocking is a software testing technique that involves creating simulated components, known as mocks or stubs, to mimic the behavior of real components in a system. The purpose of such testing is to isolate and evaluate specific parts of a software application by replacing real dependencies with mocks or stubs.
Full documentation is on official website: scalamock.org
ScalaMock 7 works only with scala 3.4+ More details about it here #567, but shortly:
- scalamock 7 uses
TupledFunction
and it currently can't be used without-experimental
compiler flag, which won't be backported to LTS 3.3 - scalamock 6 takes anvantage of scala compiler bug allowing to omit annotating everything as
@experimental
scalaVersion := "3.4.3" // or higher
Test / scalacOptions += "-experimental"
Offers you:
- Concise and powerful syntax with no complexity overhead
- Data based approach. You can get arguments or number of times method was called
- No exceptions thrown
- Support for functional effects like
ZIO
orcats-effect IO
libraryDependencies ++= Seq(
// core module
"org.scalamock" %% "scalamock" % "<latest version in badge>",
// zio integration
"org.scalamock" %% "scalamock-zio" % "latest version in badge",
// cats-effect integration
"org.scalamock" %% "scalamock-cats-effect" % "latest version in badge"
)
Quickstart is here.
ZIO/CE integration is here.
Examples:
test("drawline interaction with turtle") {
// Create mock Turtle object
val m = mock[Turtle]
// Set expectations
m.setPosition.expects(10.0, 10.0).returns(10.0, 10.0)
m.forward.expects(5.0).returns(())
m.getPosition.expects().returns(15.0, 10.0)
// Exercise System Under Test
drawLine(m, (10.0, 10.0), (15.0, 10.0))
}
test("drawline interaction with turtle") {
// Create stub Turtle
val m = stub[Turtle]
// Setup return values
m.getPosition.when().returns(15.0, 10.0)
// Exercise System Under Test
drawLine(m, (10.0, 10.0), (15.0, 10.0))
// Verify expectations met
m.setPosition.verify(10.0, 10.0)
m.forward.verify(5.0)
}
A more complete example is on our Quickstart page.
- Fully typesafe
- Full support for Scala features such as:
- Polymorphic (type parameterised) methods
- Operators (methods with symbolic names)
- Overloaded methods
- Type constraints
- ScalaTest and Specs2 integration
- Mock and Stub support
- Macro Mocks and JVM Proxy Mocks
- Scala.js support
- built for Scala 2.12, 2.13, 3
- Scala 2.10 support was included up to ScalaMock 4.2.0
- Scala 2.11 support was included up to ScalaMock 5.2.0
- Scala 2.12 and 2.13 support was included up to ScalaMock 6.1.1
Artefacts are published to Maven Central and Sonatype OSS.
For ScalaTest, to use ScalaMock in your Tests, add the following to your build.sbt
:
libraryDependencies += Seq(
"org.scalamock" %% "scalamock" % "5.2.0" % Test,
"org.scalatest" %% "scalatest" % "3.2.0" % Test
)
- Type should be specified for methods with by-name parameters
trait TestTrait:
def byNameParam(x: => Int): String
val t = mock[TestTrait]
// this one no longer compiles
(t.byNameParam _).expects(*).returns("")
// this one should be used instead
(t.byNameParam(_: Int)).expects(*).returns("")
- Not initialized vars are not supported anymore, use
scala.compiletime.uninitialized
instead - Vars are not mockable anymore
trait X:
var y: Int // No longer compiles
var y: Int = scala.compile.uninitialized // Should be used instead
Mocking of non-abstract java classes is not available without workaround
public class JavaClass {
public int simpleMethod(String b) { return 4; }
}
val m = mock[JavaClass] // No longer compiles
class JavaClassExtended extends JavaClass
val mm = mock[JavaClassExtended] // should be used instead
For usage in Maven or Gradle, integration with Specs2, and more example examples see the User Guide
YourKit is kindly supporting open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .NET Profiler.
Many thanks to Jetbrains for providing us with an OSS licence for their fine development tools such as IntelliJ IDEA.
Also, thanks to https://github.com/fthomas/scala-steward for helping to keep our dependencies updated automatically.