Navigation Menu

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

Closed
wimdeblauwe opened this issue Oct 31, 2020 · 4 comments
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@wimdeblauwe
Copy link
Contributor

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 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 #12470

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 31, 2020
@wimdeblauwe
Copy link
Contributor Author

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

@wilkinsona wilkinsona self-assigned this Nov 2, 2020
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 2, 2020
@wilkinsona wilkinsona added this to the 2.4.0 milestone Nov 2, 2020
@okrammer
Copy link

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.

@wilkinsona
Copy link
Member

@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.

@ianbrandt
Copy link

#34629 might cover @okrammer's reported issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants