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.

Real method on mocked object is always called

See original GitHub issue

I am trying to mock a class from a kotlin library with org.mockito:mockito-inline:2.7.19:

class TheTest {
    val kit: AndroidKit = mock()
    val resources: Resources = mock()

    @Test
    fun stepsIntoFunctionOfMockedClass() {
        given(kit.getResource(resources, 123, String::class)).willReturn("resource")
    }
}

When I do this first the test runs fine:

given(resources.getString(any())).willReturn("")

In the given statement the real function kit.getResource() is called which leads to an NPE since function calls on resources are not mocked. Any ideas?

Sample project to reproduce androidKit library

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:19
  • Comments:25 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
rcgonzalezfcommented, Jul 10, 2020

@craigmiller160 I had the same issue, the problem is the method is final, if you can set the method to open the issue will go away. Isn’t ideal but is a workaround that I used for a class. I think the solution will be that Mockito should complain about the final method/function instead of calling the real method.

3reactions
SarShalomSimchicommented, Jul 15, 2021

I’m having the same issue with java

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito mocking a method calls the actual method
I am trying to mock method createInstanceB() using Mockito.when() as well as doReturn() . This always calls real method. ... import org.mockito.
Read more >
Mockito – Call a Real Method - Apps Developer Blog
Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call...
Read more >
Difference between Spy and Mock thenCallRealMethod
When we use Mock the actual object instance is not created but bare-bones shell instance of the Class is created to track interactions....
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
With Mockito, you create a mock, tell Mockito what to do when specific methods are called on it, and then use the mock...
Read more >
Methods of Mockito - Javatpoint
mock() method with Answer: It is used to create mock objects of a class or interface with a specific procedure. It is an...
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