Search code examples
playframeworkmockitoplayframework-2.0scalatestplayframework-2.2

Using MockitoSugar of scalatestplus-play deprecated


I'm using "scalatestplus-play" % "5.1.0", and I'm trying to bring MockitoSugar to my class like I did in earlier projects:

class MyTestSpec extends AnyFreeSpec with MockitoSugar

so I can use the mock[MyClass] function and now it docent exists, can't find in the documentation something else.

in my earlier projects I used "scalatestplus-play" % "4.0.3", I'm sure there is a different way now but can't find what it is


Solution

  • First option you have, is to use scalatestplus-mockito, where you can find MockitoSugar, as you are used to.

    From ScalaTest Testing with mock objects:

    You can use any Java mocking framework with ScalaTest, or ScalaMock, a Scala mocking alternative. ScalaTest provides just enough syntax sugar for the three most popular Java mocking frameworks—JMock, EasyMock, and Mockito—to remove boilerplate and clarify the client code.

    Another option, as said in the attached link, there are 4 libraries that can be used. The easiest one in my opinion is ScalaMock. To use it you need to extend MockFactory.

    It should be imported from:

    import org.scalamock.scalatest.MockFactory
    

    The difference is that "scalatestplus-play" % "4.0.3" depends on ScalaTest version 3.0.8 which still had MockitoSugar. "scalatestplus-play" % "5.1.0" depends on ScalaTest 3.1.1 which no longer has it.