@@ -23,6 +23,7 @@ The library has independent developers, release cycle and versioning from core m
23
23
* Artifact identifier: "org.mockito: mockito-scala_ [ scala-version] :[ version] "
24
24
* Artifact identifier: "org.mockito: mockito-scala-scalatest_ [ scala-version] :[ version] "
25
25
* Artifact identifier: "org.mockito: mockito-scala-specs2_ [ scala-version] :[ version] "
26
+ * Artifact identifier: "org.mockito: mockito-scala-cats_ [ scala-version] :[ version] "
26
27
* Latest version - see [ release notes] ( /docs/release-notes.md )
27
28
* Repositories: [ Maven Central] ( https://search.maven.org/search?q=mockito-scala ) or [ JFrog's Bintray] ( https://bintray.com/mockito/maven/mockito-scala )
28
29
@@ -519,6 +520,68 @@ If you want to customise the print of any type you just need to declare your `Pr
519
520
}
520
521
```
521
522
523
+ ## Cats integration
524
+ By adding the module `mockito-scala-cats` 2 new traits are available, `IdiomaticMockitoCats` and `MockitoCats` which are meant to be mixed-in in
525
+ tests that use `IdiomaticMockito` and `MockitoSugar` respectively.
526
+ Please look at the [tests](/cats/src/test) for more detailed examples
527
+
528
+ ### MockitoCats
529
+ This traits adds `whenF()` which allows stubbing methods that return an Applicative (or an ApplicativeError) to be stubbed by just providing
530
+ the content of said applicative (or the error).
531
+ So for
532
+ ```scala
533
+ trait Foo {
534
+ def returnsOption[T](v: T): Option[T]
535
+ def returnsMT[M[_], T](v: T): M[T]
536
+ }
537
+ // We can now write
538
+ val aMock = mock[Foo]
539
+ whenF(aMock.returnsOption(*)) thenReturn "mocked!"
540
+ whenF(aMock.returnsMT[Future, String](*)) thenReturn "mocked!"
541
+ // Rather than
542
+ when(aMock.returnsOption(*)) thenReturn Some("mocked!")
543
+ when(aMock.returnsMT[Future, String](*)) thenReturn Future.successful("mocked!")
544
+
545
+ //We could also do stubbings in a single line if that's all we need from the mock
546
+ val inlineMock: Foo = whenF(mock[Foo].returnsOption(*)) thenReturn "mocked!"
547
+
548
+ // For errors we can do
549
+ type ErrorOr[A] = Either[Error, A]
550
+ val failingMock: Foo = whenF(mock[Foo].returnsMT[ErrorOr, MyClass](*)) thenFailWith Error("error")
551
+ //Rather than
552
+ val failingMock: Foo = when(mock[Foo].returnsMT[ErrorOr, MyClass](*)) thenReturn Left(Error("error"))
553
+ ```
554
+
555
+ The trait also provides and implicit conversion from `cats.Eq` to `scalactic.Equality` so if you have an implicit `cats.Eq` instance in scope,
556
+ it will be automatically used by the `eqTo` matcher.
557
+
558
+ ### IdiomaticMockitoCats
559
+
560
+ Similar to `MockitoCats` but for the idiomatic syntax (including the conversion from `cats.Eq` to `scalactic.Equality`), so the code would look like
561
+
562
+ ```scala
563
+ trait Foo {
564
+ def returnsOption[T](v: T): Option[T]
565
+ def returnsMT[M[_], T](v: T): M[T]
566
+ }
567
+ // We can now write
568
+ val aMock = mock[Foo]
569
+ aMock.returnsOption(*) shouldReturnF "mocked!"
570
+ aMock.returnsMT[Future, String](*) shouldReturnF "mocked!"
571
+ // Rather than
572
+ aMock.returnsOption(*) shouldReturn Some("mocked!")
573
+ aMock.returnsMT[Future, String](*) shouldReturn Future.successful("mocked!")
574
+
575
+ //We could also do stubbings in a single line if that's all we need from the mock
576
+ val inlineMock: Foo = mock[Foo].returnsOption(*) shouldReturnF "mocked!"
577
+
578
+ // For errors we can do
579
+ type ErrorOr[A] = Either[Error, A]
580
+ val failingMock: Foo = mock[Foo].returnsMT[ErrorOr, MyClass](*) shouldFailWith Error("error")
581
+ //Rather than
582
+ val failingMock: Foo = mock[Foo].returnsMT[ErrorOr, MyClass](*) shouldReturn Left(Error("error"))
583
+ ```
584
+
522
585
## Notes
523
586
524
587
### Dead code warning
0 commit comments