Skip to content

Commit 8c4823a

Browse files
committed
2 parents 57ce308 + c79db32 commit 8c4823a

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/main/scala/org/scalatestplus/mockito/MockitoSugar.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.scalatestplus.mockito
1818
import org.mockito.Mockito.{mock => mockitoMock}
1919
import reflect.ClassTag
2020
import org.mockito.stubbing.Answer
21+
import org.mockito.ArgumentCaptor
2122
import org.mockito.MockSettings
2223

2324
/**
@@ -144,6 +145,38 @@ trait MockitoSugar {
144145
def mock[T <: AnyRef](name: String)(implicit classTag: ClassTag[T]): T = {
145146
mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], name)
146147
}
148+
149+
150+
/**
151+
* Invokes the <code>forClass(classToMock: Class[T])</code> method on the <code>ArgumentCaptor</code> companion object (<em>i.e.</em>, the
152+
* static <code>forClass(java.lang.Class<T> classToMock)</code> method in Java class <code>org.mockito.Mockito</code>).
153+
*
154+
* <p>
155+
* Using the Mockito API directly, you create a ArgumentCaptor with:
156+
* </p>
157+
*
158+
* <pre class="stHighlight">
159+
* val collaboratorCaptor = ArgumentCaptor.forClass(classOf[Collaborator])
160+
* </pre>
161+
*
162+
* <p>
163+
* Using this method, you can shorten that to:
164+
* </p>
165+
*
166+
* <pre class="stHighlight">
167+
* val collaboratorCaptor = capture[Collaborator]
168+
* </pre>
169+
*/
170+
def capture[T <: AnyRef](implicit classTag: ClassTag[T]): ArgumentCaptor[T] = {
171+
ArgumentCaptor.forClass(classTag.runtimeClass.asInstanceOf[Class[T]])
172+
}
173+
174+
/**
175+
* Invoke the <code>capture(): T</code> method on the <code>ArgumentCaptor</code> for convenience.
176+
*/
177+
implicit def invokeCaptureOnArgumentCaptor[T](captor: ArgumentCaptor[T]): T = {
178+
captor.capture()
179+
}
147180
}
148181

149182
/**

src/test/scala/org/scalatestplus/mockito/MockitoSugarSpec.scala

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ class MockitoSugarSpec extends AnyFunSuite with MockitoSugar {
3131
private val listeners = new scala.collection.mutable.ListBuffer[Collaborator]
3232

3333
def addListener(listener: Collaborator): Unit = {
34-
listeners += listener
34+
listeners += listener
3535
}
3636

3737
def addDocument(name: String, bytes: Array[Byte]): Unit = {
3838
if (docs.contains(name))
3939
listeners.foreach(_.documentChanged(name))
4040
else {
41-
docs += name
41+
docs += name
4242
listeners.foreach(_.documentAdded(name))
43-
}
43+
}
4444
}
4545

4646
}
@@ -64,4 +64,28 @@ class MockitoSugarSpec extends AnyFunSuite with MockitoSugar {
6464
verify(mockCollaborator, times(3)).documentChanged("Document")
6565
}
6666

67-
}
67+
test("MockitoSugar should capture argument for testing") {
68+
// First, create the mock object
69+
val mockCollaborator = mock[Collaborator]
70+
71+
// Create the class under test and pass the mock to it
72+
val classUnderTest = new ClassUnderTest
73+
classUnderTest.addListener(mockCollaborator)
74+
75+
// Use the class under test
76+
classUnderTest.addDocument("Document", new Array[Byte](0))
77+
78+
// Create the captor
79+
val strCaptor = capture[String]
80+
val strCaptor2 = capture[String]
81+
82+
// Then verify the class under test used the mock object as expected
83+
verify(mockCollaborator).documentAdded(strCaptor.capture())
84+
assert(strCaptor.getValue === "Document")
85+
86+
// Use implicit conversion to omit "capture" method
87+
verify(mockCollaborator).documentAdded(strCaptor2)
88+
assert(strCaptor2.getValue === "Document")
89+
}
90+
91+
}

0 commit comments

Comments
 (0)