Skip to content

Commit d1dd616

Browse files
committed
Fix README for MockitoFixture, add IdiomaticMockitoFixture as well
1 parent c217a01 commit d1dd616

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

README.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,19 @@ Both `Captor[T]` and `ValCaptor[T]` return an instance of `ArgCaptor[T]` so the
112112
## `org.mockito.MockitoScalaSession`
113113

114114
This is a wrapper around `org.mockito.MockitoSession`, it's main purpose (on top of having a Scala API)
115-
is to filter out the `$default$` stubbings so they are not wrongly reported when we use Strict Stubs
115+
is to improve the search of mis-used mocks and unexpected invocations to reduce debugging effort when something doesn't work
116116

117-
To use it just create an instance of it before your test code and call `finishMocking()` when your test is done, e.g.
117+
To use it just wrap your code with it, e.g.
118118
```scala
119-
val session = MockitoScalaSession()
120-
121-
val foo = mock[Foo]
122-
when(foo.bar("pepe")) thenReturn "mocked"
123-
foo.bar("pepe") shouldBe "mocked"
124-
125-
session.finishMocking()
119+
MockitoScalaSession() {
120+
val foo = mock[Foo]
121+
when(foo.bar("pepe")) thenReturn "mocked"
122+
foo.bar("pepe") shouldBe "mocked"
123+
}
126124
```
125+
That's it! that block of code will execute within a session and will handle
127126

128-
## `org.mockito.integrations.scalatest.MockitoFixture`
127+
## MockitoFixture
129128

130129
For a more detailed explanation read [this](https://medium.com/@bbonanno_83496/introduction-to-mockito-scala-part-3-383c3b2ed55f)
131130

@@ -139,6 +138,12 @@ the mockito-scala API available in one go, i.e.
139138
class MyTest extends WordSpec with MockitoFixture
140139
```
141140

141+
In case you want to use the Idiomatic Syntax just do
142+
143+
```scala
144+
class MyTest extends WordSpec with IdiomaticMockitoFixture
145+
```
146+
142147
## `org.mockito.integrations.scalatest.ResetMocksAfterEachTest`
143148

144149
Inspired by [this](https://stackoverflow.com/questions/51387234/is-there-a-per-test-non-specific-mock-reset-pattern-using-scalaplayspecmockito) StackOverflow question,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.mockito.integrations.scalatest
2+
3+
import org.mockito._
4+
import org.scalatest.{Outcome, Suite, TestSuite}
5+
6+
trait IdiomaticMockitoFixture extends TestSuite with IdiomaticMockito with ArgumentMatchersSugar { this: Suite =>
7+
8+
abstract override def withFixture(test: NoArgTest): Outcome =
9+
MockitoScalaSession(name = s"MockitoFixtureSession[${test.name}]") {
10+
super.withFixture(test)
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.mockito.integrations.scalatest
2+
3+
import org.scalatest.{Matchers, WordSpec}
4+
5+
class IdiomaticMockitoFixtureTest extends WordSpec with IdiomaticMockitoFixture with Matchers {
6+
7+
class Foo {
8+
def bar(a: String) = "bar"
9+
}
10+
11+
trait Setup {
12+
val foo: Foo = mock[Foo]
13+
}
14+
15+
"MockitoFixture" should {
16+
"check the mocks were called with the right arguments" in {
17+
val foo = mock[Foo]
18+
19+
foo.bar(*) shouldReturn "mocked"
20+
21+
foo.bar("pepe") shouldBe "mocked"
22+
}
23+
24+
"work on tests with setup" in new Setup {
25+
"mocked" willBe returned by foo bar "pepe"
26+
27+
foo.bar("pepe") shouldBe "mocked"
28+
}
29+
}
30+
31+
}

0 commit comments

Comments
 (0)