I'm trying to use the @SpyBean
to mock a method of a @Component
and doesn't work. @MockBean
works (followed the example). I've tried, read and researched many ways but couldn't make it work.
Here's the example:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment. DEFINED_PORT)
@ExtendWith(SpringExtension::class)
@Provider("MyMicroService")
@PactFolder("../../../pacts")
internal class ClientContracts {
@SpyBean
private lateinit var myService: MyService
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider::class)
fun pactVerificationTestTemplate(context: PactVerificationContext) {
context.verifyInteraction()
}
@State("default", "NO_DATA")
fun toDefaultState() {
reset(processService)
}
}
(I super simplified the test function so it's easier to read, I'd be actually doing doReturn(...).when(...).blah()
)
I'm always getting the "not a mock" error, because the object is always the bean wrapped by Spring CGLIB:
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class com.blah.MyServiceImpl$$EnhancerBySpringCGLIB$$9712a2a5
at com.nhaarman.mockitokotlin2.MockitoKt.reset(Mockito.kt:36)
...
I've tried:
- with
@SpringJUnitConfig
- with a separate
@TestConfiguration
, but got resolved to same above bean - Using
Mockito.initAnnotations(this)
in a@BeforeEach
- and more, I've tried with so many combinations that I can't remember...
Is there something that I'm missing? Or an option that I don't know?