diff --git a/app/views/userGuide/testingWithMockObjects.scala.html b/app/views/userGuide/testingWithMockObjects.scala.html index 92150ff9b6..40bac94a3d 100644 --- a/app/views/userGuide/testingWithMockObjects.scala.html +++ b/app/views/userGuide/testingWithMockObjects.scala.html @@ -46,28 +46,14 @@
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 org.scalamock.scalatest.MockFactory
into your Suite
class,
@@ -97,10 +83,40 @@
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.
+ +To use macro mocks, mixorg.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