Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
JUnit rule logs warnings about unsued / misused stubs #384
When test fails, the failure might be caused by misused stubs. Hence, it might be worth to log out debugging information to the System out (e.g. misused stubs, unused stubs). For more details see documentation for MockitoHint.
The JUnit rule or the runner will potentially include following info in the output (somehow): https://gist.github.com/szczepiq/38619fd8766c66c46dff4dc2b6db06ec
I'm wondering if "Mockito.validateMockitoUsage();" should automatically print warnings. We could provide boolean parameter to control printing of the warnings.
I'm planning to rework the current behavior of JUnit rules, so that the rule by default: - on test failure print the stubbing mismatch - when test passes show unused stubs It will use the new StubbingListener interface. WarningsCollector class and friends will be deleted.
Work in progress for #384
Reporting needs to be based on the actual invocation, not based on String representation of the location. Issue #384
- more cases covered - more coverage Issue #384
@szczepiq: I like this feature very much. Also that the
We often have quite complex test fixtures, and it's cumbersome to switch absolutely each and every stub on or off... some methods simply have to be there; it's also not necessary to verify if they've been invoked. So it would be great to be able to disable it for some stubs, but leave it on for others.
The MockitoHint documentation seems to indicate that this will give you warnings about mismatched arguments, when using the MockitoJUnitRunner. However, from my experience this doesn't seem to work with Mockito 2.1.
Here's the source I'm trying:
Foo foo = mock(Foo.class); when(foo.bar("baz")).thenReturn("bar"); foo.bar("baz"); foo.bar("test"); // expected to get a warning here
Is there something else I need to do to enable this functionality?
Thank you for feedback!
This is how they were implemented. Runner should also report mismatches because it helps with debugging. The reason it does not do it today is because I wanted to limit the noise.
We will fix this issue. Also, we will consider always printing warning when stub args mismatch, even if one does not use runner / rule.
Do you want to open a separate ticket to track this improvement?
Thanks for reporting!
Came across this after running into this in my test :
It is complaining about this (line 584):
Now my issue is this: this particular method
Yes. For example, you can use
Hope that helps!
Thanks for the
That's more work and more white box testing than I want. Let's say this is for testing the SSL logic where peerHost (of a connection) is matched (or not matched) against "subject-name" in a cert under various conditions but I have common setup/initialization where I "mock" the peerHost of the connection. In one of the conditions the host-name match is suppressed (i.e.