0

I'm trying to verify that a logger gets called. The problem is that it returns void and so it's recommended that I use a spy instead of a stub. Unfortunately, the spy is acting funky (not working). Ideally, I would also like to remove the "msg" and use a matcher.

The code is below. I've also tried the approach given by https://stackoverflow.com/a/24150445/555493 and I still get a similar error.

//setup spy
val logger = spy(new Logger)
logger.debug(msg)

//call code

//verify
there was one(logger).debug(msg)

I'm getting this error:

The mock was not called as expected: 
Argument(s) are different! Wanted:
logger.debug(
 ($anonfun$apply$mcV$sp$1) <function0>
);
-> at HttpHealthCheckSpec$$anonfun$35$$anon$11$$anonfun$11.apply$mcV$sp(HttpHealthCheckSpec.scala:176)
Actual invocation has different arguments:
logger.debug(
 ($anonfun$setupMock$1) <function0>
);
-> at HttpHealthCheckSpec.setupMock(HttpHealthCheckSpec.scala:58)
Community
  • 1
  • 1
U Avalos
  • 6,538
  • 7
  • 48
  • 81

1 Answers1

0

It seems that the issue is that the logger accepts a String that is a call-by-name argument. As per https://stackoverflow.com/a/2158709/555493, the solution is rather ugly. Unfortunately, Mockito cannot mock Strings. So another solution is to use specs2 where Mockito has been fixed to correctly match call-by-name arguments.

Community
  • 1
  • 1
U Avalos
  • 6,538
  • 7
  • 48
  • 81