Real method on mocked object is always called
See original GitHub issueI 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?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:19
- Comments:25 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 aworkaround
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.I’m having the same issue with java