Skip to content

Commit 7056fd3

Browse files
authored
Merge pull request #179 from mockito/issue/175
Add syntax for `doNothing`
2 parents b7b35d9 + fdd7fe6 commit 7056fd3

File tree

16 files changed

+53
-16
lines changed

16 files changed

+53
-16
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ doAnswer(_ => "mocked!").when(aMock).bar <=> "mocked!" wi
323323
doAnswer(_.getArgument[Int](0) * 10).when(aMock).bar(any) <=> ((i: Int) => i * 10) willBe answered by aMock.bar(*)
324324
doCallRealMethod.when(aMock).bar <=> theRealMethod willBe called by aMock.bar
325325
doThrow(new IllegalArgumentException).when(aMock).bar <=> new IllegalArgumentException willBe thrown by aMock.bar
326+
327+
doNothing().when(aMock).bar <=> aMock.bar.doesNothing()
326328

327329
verifyZeroInteractions(aMock) <=> aMock wasNever called
328330
verify(aMock).bar <=> aMock.bar was called

core/src/main/scala-2.11/org/mockito/matchers/EqMatchers_VersionSpecific.scala

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.mockito.matchers
22

33
trait EqMatchers_VersionSpecific {
4-
54
/**
65
* Creates a matcher that delegates on {{org.scalactic.Equality}} so you can always customise how the values are compared
76
* Also works with value classes
@@ -14,5 +13,4 @@ trait EqMatchers_VersionSpecific {
1413
*/
1514
@deprecated("Use 'eqTo' instead", since = "1.0.2")
1615
def eqToVal[T](value: T): T = macro MacroMatchers_211.eqToValMatcher[T]
17-
1816
}

core/src/main/scala-2.12/org/mockito/matchers/EqMatchers_VersionSpecific.scala

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import org.mockito.{ ArgumentMatchers => JavaMatchers }
55
import org.scalactic.{ Equality, Prettifier }
66

77
trait EqMatchers_VersionSpecific {
8-
98
/**
109
* Creates a matcher that delegates on {{org.scalactic.Equality}} so you can always customise how the values are compared
1110
* Also works with value classes

core/src/main/scala/org/mockito/IdiomaticMockitoBase.scala

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ trait IdiomaticMockitoBase extends MockitoEnhancer with ScalacticSerialisableHac
112112

113113
//noinspection AccessorLikeMethodIsUnit
114114
def isLenient(): Unit = macro WhenMacro.isLenient[T]
115+
116+
def shouldDoNothing(): Unit = macro DoSomethingMacro.doesNothing
117+
def mustDoNothing(): Unit = macro DoSomethingMacro.doesNothing
118+
def doesNothing(): Unit = macro DoSomethingMacro.doesNothing
115119
}
116120

117121
implicit class VerifyingOps[T](stubbing: T) {

macro/src/main/scala-2.11/org.mockito.matchers/MacroMatchers_211.scala

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import org.scalactic.{ Equality, Prettifier }
77
import scala.reflect.macros.blackbox
88

99
object MacroMatchers_211 {
10-
1110
def eqTo[T: Equality: ValueClassExtractor](value: T)(implicit $pt: Prettifier): T = {
1211
JavaMatchers.argThat(new EqTo[T](value))
1312
value

macro/src/main/scala/org/mockito/DoSomethingMacro.scala

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ import org.mockito.Utils._
55
import scala.reflect.macros.blackbox
66

77
object DoSomethingMacro {
8+
def doesNothing(c: blackbox.Context)(): c.Tree = {
9+
import c.universe._
10+
11+
val r = c.macroApplication match {
12+
case q"$_.StubbingOps[$_]($invocation).shouldDoNothing()" =>
13+
transformInvocation(c)(invocation, q"_root_.org.mockito.MockitoSugar.doNothing")
14+
case q"$_.StubbingOps[$_]($invocation).mustDoNothing()" =>
15+
transformInvocation(c)(invocation, q"_root_.org.mockito.MockitoSugar.doNothing")
16+
case q"$_.StubbingOps[$_]($invocation).doesNothing()" =>
17+
transformInvocation(c)(invocation, q"_root_.org.mockito.MockitoSugar.doNothing")
18+
19+
case o => throw new Exception(s"Couldn't recognize '${show(o)}'")
20+
}
21+
if (c.settings.contains("mockito-print-when")) println(show(r))
22+
r
23+
}
24+
825
def returnedBy[T: c.WeakTypeTag, S](c: blackbox.Context)(stubbing: c.Expr[T])($ev: c.Expr[S]): c.Expr[S] = {
926
import c.universe._
1027

scalatest/src/test/scala-2.11/user/org/mockito/MockitoSugarTest_211.scala

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.mockito.{ ArgumentMatchersSugar, MockitoSugar }
44
import org.scalatest.{ Matchers, WordSpec }
55

66
class MockitoSugarTest_211 extends WordSpec with MockitoSugar with Matchers with ArgumentMatchersSugar {
7-
87
trait Baz {
98
def traitMethod(defaultArg: Int = 30, anotherDefault: String = "hola"): Int = ???
109
}

scalatest/src/test/scala-2.11/user/org/mockito/stubbing/ReturnsEmptyValuesTest.scala

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import scala.concurrent.Future
1212
import scala.util.{ Success, Try }
1313

1414
class ReturnsEmptyValuesTest extends WordSpec with Matchers with IdiomaticMockito with TryValues with OptionValues with ScalaFutures {
15-
1615
class KnownTypes {
1716
def returnsOption: Option[String] = Some("not mocked!")
1817
def returnsList: List[String] = List("not mocked!")

scalatest/src/test/scala-2.12/user/org/mockito/IdiomaticMockitoTest_212.scala

-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import scala.concurrent.ExecutionContext.Implicits.global
99
import scala.concurrent.Future
1010

1111
class IdiomaticMockitoTest_212 extends WordSpec with Matchers with IdiomaticMockito with ArgumentMatchersSugar with ScalaFutures {
12-
1312
class Foo {
1413
def valueClass(n: Int, v: ValueClass): String = ???
1514

1615
def valueCaseClass(n: Int, v: ValueCaseClassInt): String = ???
1716
}
1817

1918
"value class matchers" should {
20-
2119
"eqTo macro works with new syntax" in {
2220
val aMock = mock[Foo]
2321

scalatest/src/test/scala-2.12/user/org/mockito/MockitoSugarTest_212.scala

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import scala.concurrent.Future
1010

1111
//noinspection RedundantDefaultArgument
1212
class MockitoSugarTest_212 extends WordSpec with MockitoSugar with Matchers with ArgumentMatchersSugar with TableDrivenPropertyChecks with ScalaFutures {
13-
1413
val scenarios = Table(
1514
("testDouble", "foo", "baz"),
1615
("mock", () => mock[Foo], () => mock[Baz]),
@@ -19,7 +18,6 @@ class MockitoSugarTest_212 extends WordSpec with MockitoSugar with Matchers with
1918

2019
forAll(scenarios) { (testDouble, foo, baz) =>
2120
testDouble should {
22-
2321
"work with default arguments in traits" in {
2422
val testDouble = baz()
2523

scalatest/src/test/scala-2.12/user/org/mockito/scalatest/AsyncIdiomaticMockitoTest_212.scala

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.scalatest.{ AsyncWordSpec, Matchers }
66
import scala.concurrent.Future
77

88
class AsyncIdiomaticMockitoTest_212 extends AsyncWordSpec with Matchers with AsyncIdiomaticMockito {
9-
109
"AsyncMockito" should {
1110
"work with specialised methods" in {
1211
val mockFunction = mock[() => Int]

scalatest/src/test/scala-2.12/user/org/mockito/stubbing/ReturnsEmptyValuesTest.scala

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import scala.concurrent.Future
1212
import scala.util.{ Success, Try }
1313

1414
class ReturnsEmptyValuesTest extends WordSpec with Matchers with IdiomaticMockito with TryValues with OptionValues with ScalaFutures {
15-
1615
class KnownTypes {
1716
def returnsOption: Option[String] = Some("not mocked!")
1817
def returnsList: List[String] = List("not mocked!")

scalatest/src/test/scala/user/org/mockito/IdiomaticMockitoTest.scala

+8
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,14 @@ class IdiomaticMockitoTest extends AnyWordSpec with Matchers with IdiomaticMocki
908908
}
909909

910910
"mock" should {
911+
"stub a no op call" in {
912+
val org = mock[Org]
913+
914+
org.unit().doesNothing()
915+
916+
org.unit() shouldBe ()
917+
}
918+
911919
"stub a real call" in {
912920
val org: Org = mock[Org].bar shouldCall realMethod
913921
org.bar shouldBe "not mocked"

scalatest/src/test/scala/user/org/mockito/MockitoScalaSessionTest.scala

+19-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class MockitoScalaSessionTest extends AnyWordSpec with IdiomaticMockito with Mat
4242
def userClassFinal: BarFinal = new BarFinal
4343

4444
def finalFinalEqualsAndHashcode: FinalEqualsAndHashcode = ???
45+
46+
def unit(): Unit = ()
4547
}
4648

4749
class Bar {
@@ -108,6 +110,14 @@ class MockitoScalaSessionTest extends AnyWordSpec with IdiomaticMockito with Mat
108110
aFoo.bar(*) returns "mocked"
109111
}
110112
}
113+
114+
an[UnnecessaryStubbingException] should be thrownBy {
115+
MockitoScalaSession().run {
116+
val aFoo = foo()
117+
118+
aFoo.unit().doesNothing()
119+
}
120+
}
111121
}
112122

113123
"check incorrect stubs" in {
@@ -139,15 +149,21 @@ class MockitoScalaSessionTest extends AnyWordSpec with IdiomaticMockito with Mat
139149
}
140150

141151
"check unexpected invocations" in {
142-
val thrown = the[UnexpectedInvocationException] thrownBy {
152+
(the[UnexpectedInvocationException] thrownBy {
143153
MockitoScalaSession().run {
144154
val aFoo = foo()
145155

146156
aFoo.bar("pepe")
147157
}
148-
}
158+
}).getMessage should startWith("Unexpected invocations found")
149159

150-
thrown.getMessage should startWith("Unexpected invocations found")
160+
(the[UnexpectedInvocationException] thrownBy {
161+
MockitoScalaSession().run {
162+
val aFoo = foo()
163+
164+
aFoo.unit()
165+
}
166+
}).getMessage should startWith("Unexpected invocations found")
151167
}
152168

153169
"not check unexpected invocations if the call was verified" in {

scalatest/src/test/scala/user/org/mockito/TestModel.scala

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class Org {
113113
def option(a: String, b: Int): Option[String] = None
114114

115115
def printTaggedValue(value: TaggedValue[String]): String = "not mocked"
116+
117+
def unit(): Unit = ???
116118
}
117119

118120
case class Baz2(param1: Int, param2: String)

version.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#Version of the produced binaries. This file is intended to be checked-in.
22
#It will be automatically bumped by release automation.
3-
version=1.9.1
3+
version=1.10.0
44
previousVersion=1.9.0

0 commit comments

Comments
 (0)