Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockBean not reset when using constructor injection #109

Open
maschmi opened this issue Jan 27, 2024 · 0 comments
Open

MockBean not reset when using constructor injection #109

maschmi opened this issue Jan 27, 2024 · 0 comments

Comments

@maschmi
Copy link

maschmi commented 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 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

@maschmi maschmi changed the title MockBean not reset MockBean not reset when using constructor injection Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant