Skip to content

Commit 9cd56e3

Browse files
authored
Merge pull request #90 from mockito/fix-manual-arg-capture
Avoid wrapping captor.capture into eqTo
2 parents 1f378bd + be05fc7 commit 9cd56e3

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The most popular mocking framework for Java, now in Scala!!!
1616
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/mockito-scala/)
1717
## Why separate project?
1818

19-
The library has independent developers, release cycle and versioning from core mockito library (https://github.com/mockito/mockito). This is intentional because core Mockito developers don't use Scala and cannot confidently review PRs, and set the vision for the Scala library.
19+
The library has independent developers, release cycle and versioning from core mockito library (<https://github.com/mockito/mockito>). This is intentional because core Mockito developers don't use Scala and cannot confidently review PRs, and set the vision for the Scala library.
2020

2121
## Dependency
2222

@@ -145,7 +145,13 @@ verify(aMock).stringArgument(captor)
145145
captor hasCaptured "it worked!"
146146
```
147147

148-
As you can see there is no need to call `capture()` nor `getValue` anymore (although they're still there if you need them)
148+
As you can see there is no need to call `capture()` nor `getValue` anymore (although they're still there if you need them as `capture` and `value` respectively)
149+
150+
The only scenario where you still have to call `capture` by hand is where the argument you want to capture is `Any` on the method signature, in that case the `implicit` conversion that automatically does the capture
151+
```scala
152+
implicit def asCapture[T](c: Captor[T]): T = c.capture
153+
```
154+
is not called as the compiler finds no need to convert `Captor[Any]` into `Any`, as it is already an instance of `Any`, given that `Any` is the parent of every type in Scala. Because of that, the type does not need any transformation to be passed in.
149155

150156
There is another constructor `ValCaptor[T]` that should be used to capture value classes
151157
**NOTE: Since version 1.0.2 `ValCaptor[T]` has been deprecated as `ArgCaptor[T]` now support both, standard and value classes**

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

+15
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,21 @@ class IdiomaticMockitoTest extends WordSpec with Matchers with IdiomaticMockito
351351
}
352352
}
353353

354+
"work with a captor when calling capture explicitly" in {
355+
val org = orgDouble()
356+
val argCaptor = ArgCaptor[Int]
357+
358+
org.doSomethingWithThisIntAndString(42, "test")
359+
360+
org.doSomethingWithThisIntAndString(argCaptor.capture, "test") was called
361+
362+
argCaptor hasCaptured 42
363+
364+
an[ArgumentsAreDifferent] should be thrownBy {
365+
argCaptor hasCaptured 43
366+
}
367+
}
368+
354369
"check invocation order" in {
355370
val mock1 = orgDouble()
356371
val mock2 = mock[Bar]

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

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ object Utils {
6161
case q"$_.n.=~[$_]($_)" => true
6262

6363
case q"$_.Captor.asCapture[$_]($_)" => true
64+
case q"$_.capture" => true
6465

6566
case q"($_.MacroMatchers_211.eqTo[$_](...$_): $_)" => true
6667
case q"($_($_.MacroMatchers_211.eqTo[$_](...$_)): $_)" => true

0 commit comments

Comments
 (0)