Skip to content

Add trait that resets all mocks after each test #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ the mockito-scala API available in one go, i.e.
class MyTest extends WordSpec with MockitoFixture
```

## `org.mockito.integrations.scalatest.ResetMocksAfterEachTest`

Inspired by [this](https://stackoverflow.com/questions/51387234/is-there-a-per-test-non-specific-mock-reset-pattern-using-scalaplayspecmockito) StackOverflow question,
mockito-scala provides this trait that helps to automatically reset any existent mock after each test is run
The trait has to be mixed **after** `org.mockito.MockitoSugar` in order to work, otherwise your test will not compile
The code shown in the StackOverflow question would look like this if using this mechanism

```scala
TestClass extends PlaySpec with MockitoSugar with ResetMocksAfterEachTest

private val foo = mock[Foo]

override def fakeApplication(): Application = new GuiceApplicationBuilder().overrides(bind[Foo].toInstance(foo)).build
```

The main advantage being we don't have to remember to reset each one of the mocks...

If for some reason we want to have a mock that is not reset automatically while using this trait, then it should be
created via the companion object of `org.mockito.MockitoSugar` so is not tracked by this mechanism

## Experimental features

* **by-name** arguments is currently an experimental feature as the implementation is a bit hacky and it gave some people problems
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.mockito.integrations.scalatest

import java.util.concurrent.ConcurrentHashMap

import org.mockito.stubbing.Answer
import org.mockito.{ MockCreator, MockSettings, MockitoSugar }
import org.scalatest.{ Outcome, TestSuite }

import scala.reflect.ClassTag
import scala.reflect.runtime.universe.TypeTag

trait ResetMocksAfterEachTest extends TestSuite with MockCreator { self: MockCreator =>

private val mocksToReset = ConcurrentHashMap.newKeySet[AnyRef]()

private def resetAll(): Unit = mocksToReset.forEach(MockitoSugar.reset(_))

override protected def withFixture(test: NoArgTest): Outcome = {
val outcome = super.withFixture(test)
resetAll()
outcome
}

private def addMock[T <: AnyRef](mock: T) = {
mocksToReset.add(mock)
mock
}

abstract override def mock[T <: AnyRef: ClassTag: TypeTag]: T = addMock(super.mock[T])

abstract override def mock[T <: AnyRef: ClassTag: TypeTag](defaultAnswer: Answer[_]): T =
addMock(super.mock[T](defaultAnswer))

abstract override def mock[T <: AnyRef: ClassTag: TypeTag](mockSettings: MockSettings): T =
addMock(super.mock[T](mockSettings))

abstract override def mock[T <: AnyRef: ClassTag: TypeTag](name: String): T = addMock(super.mock[T](name))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.mockito.integrations.scalatest

import org.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

class ResetMocksAfterEachTestTest extends WordSpec with MockitoSugar with ResetMocksAfterEachTest with Matchers {

trait Foo {
def bar(a: String) = "bar"
}

val foo = mock[Foo]

"ResetMocksAfterEachTest" should {

"have clean state for test 1" in {

verifyZeroInteractions(foo)

when(foo.bar("pepe")) thenReturn "mocked"

foo.bar("pepe") shouldBe "mocked"

}

"have clean state for test 2" in {

verifyZeroInteractions(foo)

when(foo.bar("pepe")) thenReturn "mocked2"

foo.bar("pepe") shouldBe "mocked2"

}

}

}