spring-boot: @MockBean combined with @Repeat results in "the field cannot have an existing value" error
I need to run Spring Test concurrently. For this reason, a separate method rule has been created. The logic of this rule (code included in reproducer) is to execute the test method multiple times in a dedicated executor.
Unfortunately injection flow does not work as excepted and throws the following exception:
org.springframework.beans.factory.BeanCreationException: Could not inject field: <xxx>;
nested exception is java.lang.IllegalStateException: The field <xxx> cannot have an existing value
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.inject(MockitoPostProcessor.java:364)
Reproducer and instruction are here: https://github.com/alexey-anufriev/spring-test-concurrent-execution-problem
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 19 (14 by maintainers)
Commits related to this issue
- Allow @MockBean to be used with Framework's @Repeat Fixes gh-27693 — committed to spring-projects/spring-boot by wilkinsona 3 years ago
I have just checked the fix and it looks promising even for my case, but anyway, I agree, JUnit 5 is the best way to go. Thank you for your support.
With that changes in https://github.com/spring-projects/spring-boot/commit/f8ef90813f839f34787f362f2712411f5d9d9359, repetition via
@Repeatwill now work. On the Spring Boot side, we have no plans to make any further changes to support concurrent execution. My opinion is that you should migrate to JUnit 5 for that.@sbrannen, thank you for this suggestion, yes, this has some limitations but still works well enough for me.
I opened https://github.com/spring-projects/spring-framework/issues/27305 to track that.
I also pushed one commit to reproduce the problem: https://github.com/alexey-anufriev/spring-test-concurrent-execution-problem/commit/7be5f8e355d6232a04c532e2a27729af917b552b
@alexey-anufriev I’d prefer to keep backtick markup out of the issue titles since it doesn’t render so well on the release notes
For reference, this was originally discussed on Gitter.