Scalamock 3. Mock overloaded method without parameter
Asked Answered
F

3

29

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.

Footboard answered 1/12, 2015 at 22:40 Comment(0)
F
51

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.

Footboard answered 2/12, 2015 at 11:25 Comment(2)
This should definitely be documented. I spent about an hour looking for it. I'm considering submitting a pull request for it in the docs.Gavra
Great help. Lucky if was quite easy to find in google.Berar
S
13

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

//noinspection ConvertibleToMethodValue
(tailer.run _: () => Unit) expects()
Sixtyfourmo answered 6/12, 2017 at 12:58 Comment(0)
H
9

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
Hawse answered 25/10, 2017 at 0:3 Comment(1)
This does compile in 2.12+ NB: IntelliJ raises a bogus compile error in-line on itPinfish

© 2022 - 2024 — McMap. All rights reserved.