Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 438
user2715478 Avatar asked Dec 01 '15 22:12

user2715478


3 Answers

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.

like image 147
user2715478 Avatar answered Nov 11 '22 01:11

user2715478


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

//noinspection ConvertibleToMethodValue
(tailer.run _: () => Unit) expects()
like image 42
Chris Suszyński Avatar answered Nov 11 '22 00:11

Chris Suszyński


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
like image 8
Carlos Avatar answered Nov 11 '22 02:11

Carlos