question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Have a way to detect unmatched calls

See original GitHub issue

Following your advice on twitter, here is my recommendation to make Mockito as a better place.

As you probably know, I’m EasyMock lead dev. But working on many projects where I haven’t picked the mocking framework, I happen to work a lot with Mockito as well.

I’m annoyed by a bunch of little things but one problem makes me want to throw my computer by the window. They might already be a solution for it but I am not aware of it. I know you worked on the verification mode lately to do more or less that but it doesn’t work when not using @Mock and not really satisfactory.

So, let’s say I’m working on legacy code with tests using Mockito. My current example is code the worked perfectly and then I did a Mockito migration. So any() isn’t matching null anymore. So my when are not matching anymore. Mockito is returning null by default and the test is failing because of that.

The problem is that I have no clue where the when was called. This is complicated legacy code and tests are not as simple as one would like. So, here is what I can do:

1- Find usage, put a breakpoint everywhere the method is called, find which call is returning null, fix the when()

But I have 20 recordings… Takes ages to find what I’m looking for

2- Convert to Easymock. The unexpected calls will fail right away. I will fix them and be done

I will restate again: The wrongly non-matching when have been a source of annoyance with Mockito for many many years now.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
henri-tremblaycommented, Apr 3, 2019

Reading #1097, my opinion:

  • unstubbed, non-void method fails immediately with “I am unstubbed!” exception

Seems ok. Not sure you need to bother yourself about void/non-void. Having void to fail can make the test more resilient to unexpected calls (assuming, the tester forgot to verify for no more interactions)

  • such method is implicitly marked as verified for the verifyNoMoreInteractions() calls

Probably a good idea

  • unused stubs are detected at the end of test and trigger failure

Yes. You might need to some verifyAllStubsWereUsed when not using rules or runner

  • requires to use doReturn API for stubbing mocks (limitation of the syntax, already known gotcha when using test “spy” or strict stubs)

This is a problem because it is needed after the fact. So you are already in front of tons of mocks and recording and don’t want to change them. You are just looking for the stupid any() that was matching null in previous versions of Mockito. That’s why I was proposing a goStrict() that will put the mock in a strict mode. Any when() or doReturn would fail after goStrict() and so will an unexpected call.

1reaction
henri-tremblaycommented, Apr 2, 2019

I’ve continued digging. Two things are helpful:

  • Mockito.mockingDetails(mock).printInvocations() will print the invocation so if you think some mock is not called as expected, you can dig into that
  • Mockito.verifyZeroInteractions(mock) to make it fail and see the unexpected invocation

They are helpful but when you have a lot of mocks it requires a code change and a bit of guessing and investigating to find out what’s failing. Not a nice a the direct failure a strict mock will allow.

Due to the Mockito architecture (no replay phase), my assumption is that isn’t possible without adding something like Mockito.goStrict(mock) before the test phase.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I handle unmatched parameters in Mockito?
The first solution that you give is good, but because the default answer applies to every method of the mock, you'll need to...
Read more >
Compare two tables and find records without matches
Use the Find Unmatched Query in Access to compare two tables and identify records in one table that don't have corresponding records in...
Read more >
Create a Find Unmatched Query in Access - YouTube
Create a query to find records in one table that don't have entries in a corresponding table. This example looks for clients that...
Read more >
Error 'Unmatched tag pair/revisioned or locked content calls ...
Unmatched tag pair/revisioned or locked content calls detected! ... be isolated in the way described above and the reduction method has to ...
Read more >
How To Tell If Someone Unmatched You On Bumble
If there has been a conversation, then those matches can also expire, but it takes longer. If you don't feel like waiting, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found