Skip to content

Commit 79c97ba

Browse files
committed
Added convenience implicits and documentation explaining the intended use
1 parent 5f236e4 commit 79c97ba

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

common/src/main/scala/org/mockito/MockitoAPI.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ private[mockito] trait MockitoEnhancer extends MockCreator {
631631

632632
/**
633633
* Spies the specified object only for the context of the block
634+
*
635+
* Automatically pulls in [[org.mockito.LeniencySettings#strictStubs strict stubbing]] behaviour via implicits.
636+
* To override this default (strict) behaviour, bring lenient settings into implicit scope;
637+
* see [[org.mockito.leniency]] for details
634638
*/
635639
def withObjectSpied[O <: AnyRef: ClassTag](block: => Any)(implicit leniency: LeniencySettings, $pt: Prettifier): Unit = {
636640
val settings = leniency(Mockito.withSettings().defaultAnswer(CALLS_REAL_METHODS))

common/src/main/scala/org/mockito/mockito.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,4 +613,27 @@ package object mockito {
613613
new Equality[T] with Serializable {
614614
override def areEqual(a: T, b: Any): Boolean = Equality.default[T].areEqual(a, b)
615615
}
616+
617+
/**
618+
* Implicit [[org.mockito.LeniencySettings LeniencySettings]] provided here for convenience
619+
*
620+
* Neither are in implicit scope as is; pull one or the other in to activate the respective semantics, for
621+
* example:
622+
*
623+
* {{{
624+
* import org.mockito.leniency.lenient
625+
*
626+
* withObjectSpied[SomeObject.type] {
627+
* SomeObject.getExternalThing returns "external-thing"
628+
* SomeObject.getOtherThing returns "other-thing"
629+
* SomeObject.getExternalThing should be("external-thing")
630+
* }
631+
* }}}
632+
*
633+
* Note: strict stubbing is active by default via [[org.mockito.LeniencySettings#strictStubs strictStubs]]
634+
*/
635+
object leniency {
636+
implicit val strict: LeniencySettings = LeniencySettings.strictStubs
637+
implicit val lenient: LeniencySettings = LeniencySettings.lenientStubs
638+
}
616639
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ class MockitoScalaSessionTest extends AnyWordSpec with IdiomaticMockito with Mat
362362
}
363363
}
364364

365-
println(thrown.getMessage)
366-
// thrown.getMessage should startWith("Unnecessary stubbings detected")
365+
thrown.getMessage should startWith("Unnecessary stubbings detected")
367366
}
368367

369368
"check incorrect stubs after the expected one was called on a final class" in {
@@ -394,7 +393,7 @@ class MockitoScalaSessionTest extends AnyWordSpec with IdiomaticMockito with Mat
394393

395394
"successfully for uncalled lenient stubs" in {
396395
MockitoScalaSession().run {
397-
implicit val strict = LeniencySettings.lenientStubs
396+
import org.mockito.leniency.lenient
398397

399398
withObjectSpied[FooObject.type] {
400399
FooObject.stateDependantMethod returns 1234L
@@ -407,7 +406,7 @@ class MockitoScalaSessionTest extends AnyWordSpec with IdiomaticMockito with Mat
407406
"unsuccessfully for uncalled strict stubs" in {
408407
val thrown = the[UnnecessaryStubbingException] thrownBy {
409408
MockitoScalaSession().run {
410-
implicit val strict = LeniencySettings.strictStubs
409+
import org.mockito.leniency.strict
411410

412411
withObjectSpied[FooObject.type] {
413412
FooObject.stateDependantMethod returns 1234L

0 commit comments

Comments
 (0)