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 fields are not reset for JUnit 5 @Nested tests using @WebMvcTest #23984

wimdeblauwe opened this issue Oct 31, 2020 · 3 comments


Copy link

@wimdeblauwe wimdeblauwe commented Oct 31, 2020

Using Spring Boot 2.4.0-RC1, the @MockBean instance is not reset between tests like if I don't use nesting. You can view my example at

There are 2 tests there: MusicRestControllerTest and MusicRestControllerNestedTest which only differ in the fact that nesting is used or not. The normal one succeeds, the nested one fails because the mock bean is not reset.

This should normally work according to #12470

Copy link

@wimdeblauwe wimdeblauwe commented Oct 31, 2020

I also now added a MusicRestControllerNestedIntegrationTest that uses @SpringBootTest instead of @WebMvcTest, but that test also fails.

Copy link

@okrammer okrammer commented Nov 17, 2020

We stumbled over the same problem that the mocks are not cleared for nested classes. We tested spring-boot version 2.4.0 and discovered that mocks that are defined via @MockBeans on a field on the outer class are now not available for the test context that is used for inner class test methods.

Is this intentional?

We found a work-a-round by duplicating the @MockBean fields to the nested class but this makes nested classes quite unattractive for our use case.

Copy link

@wilkinsona wilkinsona commented Nov 17, 2020

@okrammer Duplicating the fields should not be necessary. We have an integration test where the @MockBean-annotated fields on the enclosing class are used by a nested class that verifies this behaviour. Without knowing more about your tests I can't really say why things apparently are not working for you. If you'd like us to spend some more time investigating, please open a new issue and provide a minimal sample that reproduces the behaviour that you've described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants