28
votes

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.

3

3 Answers

46
votes

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.

12
votes

In scala 2.12 this also works (no inspection for Intellij):

//noinspection ConvertibleToMethodValue
(tailer.run _: () => Unit) expects()
7
votes

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