-
Notifications
You must be signed in to change notification settings - Fork 130
Use mockito-scala-scalatest to enable strict mocks on all tests. #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
when(p(underlying)).thenReturn(isEnabled) | ||
when(canLogCorrelationId.logMessage(anyString(), any[CorrelationId])).thenReturn(logMsg) | ||
if (stubCanLog) when(canLogCorrelationId.logMessage(any[String], any[CorrelationId])).thenReturn(logMsg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strict mode also means that if you stub something and is not used you'll get a failure as it indicates your code is not doing what the test expected it to do, hence the flag to only stub this for the tests that do a positive verification
logger.error("This should not throw: {}, {} - {}", arg1, arg2, arg3) | ||
} | ||
verify(underlying).error(any[String], *, *, *) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to add the verification due to strict-mode (if a mock method is called, and it wasn't either stubbed nor verified the test will fail)
I had to add the 3rd param as the macro always calls the varargs version of the Slf4j class, but on the direct call to verify, the compiler would pick the overloaded non-vararg version and the verification would fail as they are effectively different methods. By using 3 arguments the problem goes away as the only option is the varargs method
} | ||
} | ||
|
||
"Calling debug with tagged args" should { | ||
|
||
"not throw ClassCastException when varargs are passed" in { | ||
val f = fixture(_.isTraceEnabled) | ||
val f = fixture(_.isDebugEnabled) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test and the next were actually not testing anything, as the incorrect log level was being enabled, thanks strict mode!! :)
f50c829
to
a0ec294
Compare
I don't know anything about Mockito... is there anyone watching this repo who can review this? (If we can't find anybody, I guess I'll merge regardless after a waiting period...) |
Inspired by the comment on #172 this PR uses the ScalaTest integration of mockito-scala so all mocks are strict by default.
An easy way to verify this would be to re-write the first test on
LoggerSpec
as (notice the extra direct call tounderlying.info
Executing such test would fail as
This change has already paid by itself as it shown that the tests on
LoggerWithTaggedArgsSpec
L#68 and L#77 were not testing anything as the wrong log level was being enabled