user2715478
user2715478

Reputation: 1293

Scalamock 3. Mock overloaded method without parameter

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.

Upvotes: 30

Views: 9378

Answers (3)

Chris Suszyński
Chris Suszyński

Reputation: 1554

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

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

Upvotes: 13

Carlos
Carlos

Reputation: 159

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

Upvotes: 9

user2715478
user2715478

Reputation: 1293

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.

Upvotes: 52

Related Questions