From 7e7c6fd11162ac00a460977b2614d40e175ec718 Mon Sep 17 00:00:00 2001 From: barkhorn Date: Thu, 16 Mar 2017 19:37:22 +0000 Subject: [PATCH 1/3] updated ScalaMock section in user guide --- .../testingWithMockObjects.scala.html | 88 +++++++------------ 1 file changed, 34 insertions(+), 54 deletions(-) diff --git a/app/views/userGuide/testingWithMockObjects.scala.html b/app/views/userGuide/testingWithMockObjects.scala.html index 92150ff9b6..ebc83b19ce 100644 --- a/app/views/userGuide/testingWithMockObjects.scala.html +++ b/app/views/userGuide/testingWithMockObjects.scala.html @@ -46,28 +46,14 @@

Using ScalaMock

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 @@

Function mocks

m expects (42) returning "Forty two" once +

Macro genrated (type-safe) mocks

+ +

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, mix 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

-

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):

@@ -131,42 +147,6 @@

Proxy mocks

m stubs 'forward - -

Generated mocks

- -

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)
-
-

Expectations

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 Date: Thu, 16 Mar 2017 19:50:10 +0000 Subject: [PATCH 2/3] fix typo --- app/views/userGuide/testingWithMockObjects.scala.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/userGuide/testingWithMockObjects.scala.html b/app/views/userGuide/testingWithMockObjects.scala.html index ebc83b19ce..a2511178ad 100644 --- a/app/views/userGuide/testingWithMockObjects.scala.html +++ b/app/views/userGuide/testingWithMockObjects.scala.html @@ -83,7 +83,7 @@

Function mocks

m expects (42) returning "Forty two" once -

Macro genrated (type-safe) mocks

+

Macro generated (type-safe) mocks

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 Date: Thu, 16 Mar 2017 19:50:42 +0000 Subject: [PATCH 3/3] fix typo --- app/views/userGuide/testingWithMockObjects.scala.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/userGuide/testingWithMockObjects.scala.html b/app/views/userGuide/testingWithMockObjects.scala.html index a2511178ad..40bac94a3d 100644 --- a/app/views/userGuide/testingWithMockObjects.scala.html +++ b/app/views/userGuide/testingWithMockObjects.scala.html @@ -52,7 +52,7 @@

Using ScalaMock