Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeseng committed Jun 20, 2024
2 parents 57ce308 + c79db32 commit 8c4823a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/main/scala/org/scalatestplus/mockito/MockitoSugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.scalatestplus.mockito
import org.mockito.Mockito.{mock => mockitoMock}
import reflect.ClassTag
import org.mockito.stubbing.Answer
import org.mockito.ArgumentCaptor
import org.mockito.MockSettings

/**
Expand Down Expand Up @@ -144,6 +145,38 @@ trait MockitoSugar {
def mock[T <: AnyRef](name: String)(implicit classTag: ClassTag[T]): T = {
mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], name)
}


/**
* Invokes the <code>forClass(classToMock: Class[T])</code> method on the <code>ArgumentCaptor</code> companion object (<em>i.e.</em>, the
* static <code>forClass(java.lang.Class<T> classToMock)</code> method in Java class <code>org.mockito.Mockito</code>).
*
* <p>
* Using the Mockito API directly, you create a ArgumentCaptor with:
* </p>
*
* <pre class="stHighlight">
* val collaboratorCaptor = ArgumentCaptor.forClass(classOf[Collaborator])
* </pre>
*
* <p>
* Using this method, you can shorten that to:
* </p>
*
* <pre class="stHighlight">
* val collaboratorCaptor = capture[Collaborator]
* </pre>
*/
def capture[T <: AnyRef](implicit classTag: ClassTag[T]): ArgumentCaptor[T] = {
ArgumentCaptor.forClass(classTag.runtimeClass.asInstanceOf[Class[T]])
}

/**
* Invoke the <code>capture(): T</code> method on the <code>ArgumentCaptor</code> for convenience.
*/
implicit def invokeCaptureOnArgumentCaptor[T](captor: ArgumentCaptor[T]): T = {
captor.capture()
}
}

/**
Expand Down
32 changes: 28 additions & 4 deletions src/test/scala/org/scalatestplus/mockito/MockitoSugarSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ class MockitoSugarSpec extends AnyFunSuite with MockitoSugar {
private val listeners = new scala.collection.mutable.ListBuffer[Collaborator]

def addListener(listener: Collaborator): Unit = {
listeners += listener
listeners += listener
}

def addDocument(name: String, bytes: Array[Byte]): Unit = {
if (docs.contains(name))
listeners.foreach(_.documentChanged(name))
else {
docs += name
docs += name
listeners.foreach(_.documentAdded(name))
}
}
}

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

}
test("MockitoSugar should capture argument for testing") {
// First, create the mock object
val mockCollaborator = mock[Collaborator]

// Create the class under test and pass the mock to it
val classUnderTest = new ClassUnderTest
classUnderTest.addListener(mockCollaborator)

// Use the class under test
classUnderTest.addDocument("Document", new Array[Byte](0))

// Create the captor
val strCaptor = capture[String]
val strCaptor2 = capture[String]

// Then verify the class under test used the mock object as expected
verify(mockCollaborator).documentAdded(strCaptor.capture())
assert(strCaptor.getValue === "Document")

// Use implicit conversion to omit "capture" method
verify(mockCollaborator).documentAdded(strCaptor2)
assert(strCaptor2.getValue === "Document")
}

}

0 comments on commit 8c4823a

Please sign in to comment.