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

Most upvoted comments

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

Those two feel contradictory to me and I think it would be good to try and clarify the javadoc.

I opened https://github.com/spring-projects/spring-framework/issues/27305 to track that.

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