It is driving me crazy why the real method getting called during setting up the mock on spy.
@Service
class A(){
fun someMethod():String{
println("inside real method")
return "result"
}
}
test:
@SpyBean
private lateinit var a:A
@Test
fun test(){
println("before mock")
Mockito.`when`(a.someMethod()).doReturn("mock_result")
println("empty test ended")
}
result:
before mock
inside real method
empty test ended
Once change from Sypbean to Mockbean then it works as expected:
@MockBean
private lateinit var a:A
@Test
fun test(){
println("before mock")
Mockito.`when`(a.someMethod()).doReturn("mock_result")
println("empty test ended")
}
result:
before mock
empty test ended
Why the real method getting called while just setting up the mock? There is no direct method invocation at all in the code, the real test not even started.
edit: I tried with doReturn,thenReturn,doAnswer,thenAnswer , the result is the same: if it is a spy then the real method getting called in the initializing phase