Ninja-Squad / springmockk

SpringMockK: MockBean and SpyBean, but for MockK instead of Mockito
Apache License 2.0
490 stars 31 forks source link

When verifying SpykBean's, verification acknowledgement fails #88

Closed slawek-mazur closed 2 years ago

slawek-mazur commented 2 years ago
@SpykBean
private lateinit var serviceHelper: ServiceHelper

in test

confirmVerified(serviceHelper)
can't find stub ServiceHelper(serviceHelper#1)
io.mockk.MockKException: can't find stub ServiceHelper(serviceHelper#1)
    at app//io.mockk.impl.stub.StubRepository.stubFor(StubRepository.kt:16)
    at app//io.mockk.impl.recording.CommonVerificationAcknowledger.acknowledgeVerified(CommonVerificationAcknowledger.kt:25)
    at app//io.mockk.MockKDsl.internalConfirmVerified(API.kt:277)
    at app//io.mockk.MockKKt.confirmVerified(MockK.kt:319)

it works perfectly fine when

@MockkBean
private lateinit var serviceHelper: ServiceHelper
jnizet commented 2 years ago

Hi @slawek-mazur

My guess is that it's the same issue as https://github.com/Ninja-Squad/springmockk/issues/85. But I'm still waiting for a minimal repro or test case allowing me to debug (and hopefully fix) the issue. Please take some time to provide one.

slawek-mazur commented 2 years ago

hello.

sure thing, please take a look at this minimal PoC

https://github.com/slawek-mazur/mockk-spykbean

it is indeed related to that enrichment $$EnhancerBySpringCGLIB$$ where CustomService becomes com.example.demo.CustomService$$EnhancerBySpringCGLIB$$

if I comment out like so 👇 there's no AOP and CGLIB doesn't kick in with it's proxies.

@Configuration
//@EnableAsync
class ExecutorConfig
jnizet commented 2 years ago

@slawek-mazur thanks for the repro. You're indeed hitting the same issue with AOP-modified Spring beans which exists since day 1 (see the first limitation listed in the README), and that I have no idea how to fix. Sorry for that.

There's a test case, BTW, that comes from Spring Boot and which is disabled because it fails, see https://github.com/Ninja-Squad/springmockk/blob/master/src/test/kotlin/com/ninjasquad/springmockk/SpyBeanWithAopProxyTests.kt#L39).

In your specific case, you should be able to workaround the issue by using confirmVerified(AopTestUtils.getUltimateTargetObject(service)) or confirmVerified(AopTestUtils.getTargetObject(service)). But this is a hack.

Unless someone can finally find a solution for this, my only advice would be to find a workaround. I generally thend to avoid spies in the first place.

slawek-mazur commented 2 years ago

Fair enough, thanks for the support.