Quick revision
The short answer
MockitoExtension runs in strict stubs mode, which reports any when(...) stub the test set up but never used, as UnnecessaryStubbingException. After a refactor it usually means the code path changed and the test's setup is out of date. The right response is to remove or correct the stub, not to switch the whole class to lenient mode, and to use lenient() only for shared setup that some tests intentionally do not use.
What to remember
In this answer, we saw that UnnecessaryStubbingException comes from MockitoExtension's strict stubs mode, which reports stubs the test never used. After a refactor it usually means the setup is out of date, and sometimes it reveals a real bug. We fix it by removing or correcting the stub, keep lenient() for individual shared stubs that some tests deliberately skip, and keep strictness on because it also reports argument mismatches at the moment they happen.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
MockitoExtension runs in strict stubs mode, which reports any when(...) stub the test set up but never used, as UnnecessaryStubbingException. After a refactor it usually means the code path changed and the test's setup is out of date. The right response is to remove or correct the stub, not to switch the whole class to lenient mode, and to use lenient() only for shared setup that some tests intentionally do not use.
Why this question is asked
This exception often appears right after a refactor that seemed harmless, and the tempting fix is to make it go away with one annotation. Interviewers ask about it because the way we respond shows whether we treat tests as documentation of behaviour or as noise to silence. In this answer, we will recap what strict stubbing is, reproduce the failure after a refactor, read what it is telling us, fix it properly, look at when lenient() is fine, and see the second, less known benefit of strictness: catching argument mismatches.
1A Quick Recap of Strict Stubs
A stub is a rule we give a mock: when(repo.findById(7)).thenReturn(Optional.of(user)) means 'if findById(7) is called, return this user'. Mockito has strictness levels that decide how it treats stubs. MockitoExtension for JUnit 5 uses STRICT_STUBS by default. At the end of each test it checks every stub the test created, and if one was never called, the test fails with UnnecessaryStubbingException, naming the line where the stub was created.
The idea behind it is that a test should contain only what it needs. A stub nobody uses is dead code that misleads readers: it suggests the code under test depends on that call when it does not.
2Reproducing It After a Refactor
Here is a test for a DiscountService that used to look up the customer's tier from a CustomerRepository. A refactor changed the service to receive the tier directly from the Order, so it no longer calls the repository.
Notice that the assertion passes. The price is right. The test fails only because of the stub on line 11, which describes a dependency that no longer exists.
3What the Failure Is Telling Us
The message is a small piece of information about the code: the path this test exercises has changed, and the test's picture of it is out of date. Most of the time that is all, and the refactor is fine. But sometimes it reveals a real problem. Perhaps the refactor accidentally bypassed a lookup that was supposed to happen, and the test still passes only because the Order in the test happens to carry the right tier.
So the first step is to read the stub and ask: does the code still need this call? If no, the stub goes. If yes, the code has a bug that the test just caught, and we fix the code. Either way, the failure did its job.
- Read the reported stub linewhich call is no longer made?
- Should the code still make this call?YesBug found: fix the codeNoSetup is out of date
- Remove or update the stubthe test now shows the real dependencies
- Stub shared by many tests on purpose?YesMark only that stub lenient()NoDone
4 steps in order. Play them, or step through them one at a time.
4Fixing It Properly
In our case the service is correct and the repository is no longer needed. We remove the stub, and since nothing uses the CustomerRepository mock any more, we remove the mock too, which also makes the constructor change visible in the test.
What we should not do is put @MockitoSettings(strictness = Strictness.LENIENT) on the class. It makes the error disappear, but it also disables the check for every test in the class, so the next stale stub, or the next real bug of this kind, goes unnoticed. Over time the setup methods fill with stubs that nobody can safely remove, because nobody knows which tests depend on them.
5When lenient() Is the Right Tool
There is one legitimate case. Some test classes have a shared @BeforeEach that prepares a common situation, and a few tests deliberately take a path that does not use part of it, for example a test for an invalid order that is rejected before any price is looked up. We could duplicate the setup in each test, but marking just that stub as lenient states the intent precisely.
Notice the scope: lenient() applies to one stub, and the rest of the class stays strict. If many stubs need lenient(), that is a sign the shared setup tries to serve too many different tests and should be split.
6Strictness Also Catches Argument Mismatches
Strict stubs have a second benefit that people often miss. If the code calls a stubbed method with different arguments from the stub, a lenient mock silently returns the default value, such as null or Optional.empty(), and the test fails later with a confusing error. Under STRICT_STUBS, Mockito reports a PotentialStubbingProblem at the moment of the call, showing the stubbed arguments and the actual ones side by side.
That is a precise pointer to the bug, instead of a NullPointerException three calls later. It is one more reason to keep strictness on and deal with what it reports.
7Follow-up questions
Why does the plain Mockito.mock() not report unused stubs?
Strictness is enforced by a Mockito session, which MockitoExtension and the MockitoJUnit rule create around each test. Mocks created with mock() inside that session are covered too. Without the extension or a session, there is no point at the end of the test where Mockito checks.
Can the same stub be used in some test runs and not others?
Yes, in parameterized tests where one case takes a different path. Then lenient() on that stub, or splitting the cases into separate tests, is a reasonable choice.
Does this apply to @MockitoBean in Spring tests?
Not in the same way. Spring's bean overrides are not managed by MockitoExtension's strictness, so unused stubs on them are not reported. That is one more reason to keep pure unit tests free of a Spring context.
8Conclusion
In this answer, we saw that UnnecessaryStubbingException comes from MockitoExtension's strict stubs mode, which reports stubs the test never used. After a refactor it usually means the setup is out of date, and sometimes it reveals a real bug. We fix it by removing or correcting the stub, keep lenient() for individual shared stubs that some tests deliberately skip, and keep strictness on because it also reports argument mismatches at the moment they happen.
Open with
It means the test set up a stub that was never called. MockitoExtension uses strict stubs by default, so at the end of each test it fails if any when stub went unused, and it names the line.
Then, if they want more
After a refactor that usually means the code path changed and the test still describes the old dependencies. My first step is to ask whether the code should still make that call. If it should, the test has just found a bug in the refactor.
If not, I delete the stub, and often the whole mock, so the test shows the real dependencies. I would not switch the class to lenient strictness, because that hides the next stale stub and the next real bug of this kind.
I use lenient() only on a specific shared stub in a BeforeEach that some tests intentionally do not reach, like a rejection test that never looks up a price. If many stubs need it, I split the setup.
I would also mention that strictness catches argument mismatches: if the code calls a stubbed method with different arguments, Mockito reports a PotentialStubbingProblem immediately, instead of returning null and failing somewhere confusing.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A test using MockitoExtension fails with UnnecessaryStubbingException after a refactor....": https://scrivio-interview-library.netlify.app/java/mocking/7fa515adc4/