I couldn't find any documentation that explains how to mock overloaded methods that take no arguments in scalamock e.g
public boolean isInfoEnabled(Marker marker);
public boolean isInfoEnabled();
To mock the function that takes the Marker, one can simply use
(loggerMock.isInfoEnabled(_: Marker)).expects(*).returning(true)
But how to mock the other method that takes no parameters? Any help is appreciated.
I finally figured it out:
(loggerMock.isInfoEnabled: () => Boolean).expects().returning(true)
This issue helped me a lot. Still would be nice to have something like this documented.
In scala 2.12 this also works (no inspection for Intellij):
//noinspection ConvertibleToMethodValue
(tailer.run _: () => Unit) expects()
I was using using this approach until I realised that in Scala 2.12+ this solution is deprecated.
You will get a warning like
Eta-expansion of zero-argument method values is deprecated.
After some researching I found out this solution:
(loggerMock.isInfoEnabled _ ).expects().returning(true)
or
import scala.language.postfixOps
loggerMock.isInfoEnabled _ expects () returning true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With