From 7e7c6fd11162ac00a460977b2614d40e175ec718 Mon Sep 17 00:00:00 2001
From: barkhorn
ScalaMock is a native, open-source Scala mocking
- framework written by Paul Butcher that allows you to mock objects and functions.
+ framework, originally written by Paul Butcher, that allows you to mock objects and functions.
ScalaMock supports three different mocking styles:
To use ScalaMock, mix This is the preferred mocking style in ScalaMock with the richest set of features. It can mock Scala traits,
+Java interfaces, and classes. There is also support for Scala.JS. Macro mocks are implemented as (compile-time generated)
+subclasses of the type to mock, which has a few limitations:
+Singletons/Companion Objects or final classes can not been mocked with this technology. Mocking a class with macro mocks means
+that all constructor code is still executed, so it is advised to mock a trait/interface instead where possible.Using ScalaMock
org.scalamock.scalatest.MockFactory
into your Suite
class,
@@ -97,10 +83,40 @@ Function mocks
m expects (42) returning "Forty two" once
+Macro genrated (type-safe) mocks
+
+org.scalamock.MockFactory
into your test suite.
+Macro mocks are created with mock
. The following, for example, creates a mock that implements
+all the Turtle
trait (interface):
+val m = mock[Turtle] ++ +
You can then set expectations on each of the methods within those traits. Here is an example:
+ ++m setPosition _ expects(10.0, 10.0) +m forward _ expects(5.0) +m getPosition _ expects() returns(15.0, 10.0) ++ +
There are a lot of additional features in ScalaMock, like being able to execute a Scala function on call to dynamically create +a return value or perform other actions. You can use mocks either in the style shown here, or more similar to +Mockito, with verifications after the code under test. Macro mocks have support for asynchronous testing with mocks, integrated with ScalaTest's +AsyncFlatSpec et al. +More information can be found in ScalaMock's User Guide.
+Proxy mocks can only be used to mock Scala traits and Java interfaces. (To mock classes, singleton/companion -objects etc., please use generated mocks.) +
Proxy mocks can only be used to mock Scala traits and Java interfaces. (To mock classes, please use macro mocks).
+Proxy mocks depend on the JVM classloader.
To use proxy mocks, mix org.scalamock.ProxyMockFactory
into your test suite.
Proxy mocks are created with mock
. The following, for example, creates a mock that implements
all the Turtle
trait (interface):
Generated mocks rely on the ScalaMock compiler plugin.
-Classes that are going to be mocked need to be declared with the org.scalamock.annotation.mock
-annotation. To mock a class together with its companion object, use
-org.scalamock.annotation.mockWithCompanion
. To mock a standalone singleton object, use
-org.scalamock.annotation.mockObject
.
In addition to MockFactory
, your test class also needs to mix in GeneratedMockFactory
.
Then, to create a regular mock object, use mock
:
-val m = mock[Turtle] - -m.expects.forward(10.0) twice -- -
To mock a singleton or companion object, use mockObject
:
-val m = mockObject(Turtle) - -m.expects.createTurtle -- -
And to mock a constructor invocation, use newInstance
:
-val m = mock[Turtle] - -m.expects.newInstance('blue) -m.expects.forward(10.0) --
You can specify expectations about the arguments with which a function or method is called and
From 8f8d84886b04f463064fdc3b6b752208e7767f38 Mon Sep 17 00:00:00 2001
From: Philipp Meyerhoefer This is the preferred mocking style in ScalaMock with the richest set of features. It can mock Scala traits,
Java interfaces, and classes. There is also support for Scala.JS. Macro mocks are implemented as (compile-time generated)
From f6863385c3cfd646aedfa89bdf39bc7adf872a3c Mon Sep 17 00:00:00 2001
From: Philipp Meyerhoefer Function mocks
m expects (42) returning "Forty two" once
-Macro genrated (type-safe) mocks
+Macro generated (type-safe) mocks
Using ScalaMock