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.

Detect unnecessary stubbings

See original GitHub issue

Mockito throws an exception when a function is stubbed and then not actually invoked.

This is particularly useful when doing TDD, or generally when writing tests first, because it allows to stub a function, have the test fail because it was not invoked, and then make it pass by implementing the invocation.

We could add a parameter to the mockk() function and the @MockK annotation, or a configuration flag to make it global, so that it works somewhat like this:

class Test {
    @MockK(detectUnnecessaryStubbing = true)
    private lateinit var collaborator: Collaborator

    private lateinit var service: Service

    @BeforeEach
    fun setup() {
        MockKAnnotations.init(this) // Perhaps this could be another good place to enable the "unnecessary stubbing detection mode"

        service = Service(collaborator)
    }

    @Test
    fun test() {
        every { collaborator.function() } returns "foo"

        service.anotherFunction() // Not executing collaborator.function in here will fail
    }
}

A potential issue I see in this is the case when there are two tests executing service.anotherFunction(), one stubbing and invoking collaborator.function() and one not stubbing nor invoking it.

We need to find a way to detect that it’s ok for the second test not to invoke collaborator.function() even if it was stubbed in another test.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

2reactions
Raibazcommented, May 11, 2022

Implemented in 1.12.4.

0reactions
xpeppercommented, Feb 8, 2021

Great, I’m willing to test a version of MockK with this new behaviour as soon as is available!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito Strict Stubbing and The UnnecessaryStubbingException
Simply put, an unnecessary stub is a stubbed method call that was never realized during test execution. Let's take a look at a...
Read more >
How to resolve Unneccessary Stubbing exception
First, you are mocking the wrong method (you made a typo or someone changed tested code so that mocked method is no longer...
Read more >
UnnecessaryStubbingException (Mockito 2.8.9 API) - javadoc.io
This exception indicates presence of unused stubbings. It is highly recommended to remove unused stubbings to keep the codebase clean.
Read more >
UnnecessaryStubbingException (Mockito 2.2.7 API)
Unnecessary stubs are stubbed method calls that were never realized during test execution (see also MockitoHint ), example: //code under test: .
Read more >
Fixing Mockito UnnecessaryStubbingException with JUnit5
JUnit 5 is tricter regarding stubbing. As explained in the error message, unnecessary stubbing is not a good practice. It usually results from ......
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