Scala 3 + Scala Test + Mockito

I want to use Scala Test togehter with Mockito (which I used back with Java).

I added the following sbt dependencies:

"org.scalatest"            %% "scalatest"               % "3.2.19" % Test,
"org.mockito"               % "mockito-core"            % "5.18.0" % Test,
"org.mockito"              %% "mockito-scala"           % "2.0.0"  % Test,
"org.mockito"              %% "mockito-scala-scalatest" % "2.0.0"  % Test

Unfortunately, those versions do not appear to be compatible, even though they are all the latest versions.

Do you have a working combination of Scala, ScalaTest, and Mockito versions?

1 Like

It seems mockito-scala doesn’t support Scala 3 (yet?); Add support for Scala 3 · Issue #364 · mockito/mockito-scala · GitHub
Thus, you can’t use this simple setup.

I would say you have three options:

  1. Use the for3Use2.13 trick… but this can have a lot of issues, especially if there are macros involved (which is likely).
  2. Use Mockito directly witjout any Scala wrapper.
  3. Contribute the support of Scala 3 for mockito-scala.
  4. Search for another Scala wrapper of Mockito, or write your own.
  5. Don’t use Mockito at all.
  6. Don’t use Scala 3.

Personally, I would suggest following a different testing style altogether, especially for a new project.
But, I won’t elaborate further since you didn’t ask about testing recommendations per se.

2 Likes

Thank you, for your answert - I think for now I’d use Mockito directly.

As we are all here to learn: What would you use? Just a short teaser, please?

1 Like

:+1:
TBH, I also think is the best option in general.


In general, I don’t write too many traditional unit tests anymore.
Rather, I would usually focus on writing scenario tests: A Philosophy of Testing 1: Introduction | by Mark "Justin" Waks | Medium using weaver.
Then I would only write unit tests for complex things, such as involved algorithms or custom data structures.

Finally, I would also usually mix that with some form of property-based testing using ScalaCheck, if it makes sense. But, using fixed inputs for known bugs / regressions.

1 Like

Thank you for the suggested reading, I will have a look into it!

1 Like

Thanks! Although I’ll footnote that the scenario-test approach tends to be optimal for testing backend services, not necessarily as much for some other sorts of problems. (Eg, when testing libraries, I’d focus much more heavily on property-based testing.)

But agreed that, if one is looking at Mockito, a scenario-testing approach is usually better in the long run: it requires more upfront investment, but pays off big as the system matures and the number of tests grows.

Mockito (and traditional unit testing in general) makes some sense in “classic OO” code, where each class is basically a complex mutable state machine that needs heavy testing in isolation. That’s traditional Java style, and you can do that in Scala (some folks do). But typical Scala idiom has gone in very different directions over the years, and most unit tests are a waste of time for the sort of strongly-typed, immutable, relatively “lightweight” classes that are more common in typical Scala code.

I’ll note that I’ve specifically been bitten quite badly by Mockito in the past. IIRC, it isn’t strongly-typed enough to really fit well with idiomatic Scala code, and is too null-friendly. I had one project back in 2017 where I lost months fixing all the Mockito tests while I was doing a big library upgrade for my then-new employer – the Mockito tests just wound up annoyingly brittle and hard to track down and fix, in a very counter-productive way.

Indeed, the scenario-testing approach really got nailed down as a reaction to that bad Mockito experience: I wanted something better for the new services I started building after that. So I took the ideas I’d been evolving for testing Querki and fleshed them out more thoroughly, eventually resulting in the above-linked articles.

1 Like

Would you like to consider ScalaMock? Since version 6.0.0, current version being 7.3.2, Scala 3 support has been added to it: https://github.com/scalamock/scalamock/pull/490

1 Like

@dubaut

Americium uses a mix of ScalaText and Mockito for some of its own tests, and is cross-built for Scala 2.13 and Scala 3.

See here for the SBT build dependencies: americium/build.sbt at 292e5ad3b082211c7d124061f23a4e69ad5b509c · sageserpent-open/americium · GitHub.

1 Like