You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using mockito-kotlin and constructor injection via @MockBean the mocked beans are not reset between tests. Default is they reset after each test. The problem does not occur when the SpringExtension is defined int he spec with override fun extensions() = listOf(SpringExtension).
This should either be added somwhere in the documentation or be fixed. One way of fixing woul be to enable the SpringExtension in the SpringAutowireConstructorExtension like
val manager = TestContextManager(clazz.java)
val context = manager.testContext.applicationContext
val autowiredSpec = context.autowireCapableBeanFactory.autowire(
clazz.java,
AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true
) as Spec
autowiredSpec.extensions(SpringExtension) // adding the Extension to the spec
autowiredSpec
However, I'm not sure if this will affect all test with constructor parameters when the kotest-extensions-spring is there. Maybe there is a way to check for spring specific annotations in the SpringAutowireConstructorExtension?
I do have the above mentioned "fix" and tests ready in a forked repository (https://github.com/maschmi/kotest-extensions-spring). However, I had to increase the target JVM version from 1.8 to 11 as this is required for mockito-kotlin to work. I was not quickly able to get ArgumentMatcher to work without this dependency. Shall I open a PR anyways?
As written above: I'm not sure if this should be fixed in the SpringAutowireConstructorExtension or merly added to the documentation that this extension does not use the SpringExtension by default. If it is only added to the documentation: Does anyone knwo how to mention it in here https://www.baeldung.com/kotlin/kotest-spring-boot-test
The text was updated successfully, but these errors were encountered:
maschmi
changed the title
MockBean not reset
MockBean not reset when using constructor injection
Jan 27, 2024
When using
mockito-kotlin
and constructor injection via@MockBean
the mocked beans are not reset between tests. Default is they reset after each test. The problem does not occur when theSpringExtension
is defined int he spec withoverride fun extensions() = listOf(SpringExtension)
.This should either be added somwhere in the documentation or be fixed. One way of fixing woul be to enable the SpringExtension in the
SpringAutowireConstructorExtension
likeHowever, I'm not sure if this will affect all test with constructor parameters when the
kotest-extensions-spring
is there. Maybe there is a way to check for spring specific annotations in theSpringAutowireConstructorExtension
?I do have the above mentioned "fix" and tests ready in a forked repository (https://github.com/maschmi/kotest-extensions-spring). However, I had to increase the target JVM version from 1.8 to 11 as this is required for mockito-kotlin to work. I was not quickly able to get ArgumentMatcher to work without this dependency. Shall I open a PR anyways?
As written above: I'm not sure if this should be fixed in the
SpringAutowireConstructorExtension
or merly added to the documentation that this extension does not use the SpringExtension by default. If it is only added to the documentation: Does anyone knwo how to mention it in here https://www.baeldung.com/kotlin/kotest-spring-boot-testThe text was updated successfully, but these errors were encountered: