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 https://github.com/wimdeblauwe/blog-example-code/tree/feature/nestedtests/nestedtests/src/test/java/com/wimdeblauwe/examples/nestedtests/music/web
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 https://github.com/spring-projects/spring-boot/issues/12470
Comment From: wimdeblauwe
I also now added a MusicRestControllerNestedIntegrationTest that uses @SpringBootTest instead of @WebMvcTest, but that test also fails.
Comment From: okrammer
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.
Comment From: wilkinsona
@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.
Comment From: ianbrandt