Skip @AutoConfigureWebTestClient when WebTestClient is built manually#1040
Merged
MBoegers merged 1 commit intoJun 17, 2026
Merged
Conversation
…lt manually When a test builds its `WebTestClient` manually (e.g. via `WebTestClient.bindToServer().build()`) the bean is not auto-configured, so `@AutoConfigureWebTestClient` is unnecessary. Adding it pulls in the `org.springframework.boot:spring-boot-webtestclient` dependency that may not be present, breaking compilation. Guard the recipe with a `UsesMethod` precondition so the annotation is only added when `WebTestClient` is not constructed manually. Partially addresses moderneinc/customer-requests#2572 (Gap 2).
timtebeek
approved these changes
Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed
AddAutoConfigureWebTestClientadds@AutoConfigureWebTestClientto@SpringBootTesttests that referenceWebTestClient. However, when a test builds itsWebTestClientmanually — e.g.WebTestClient.bindToServer().build()— the bean is not auto-configured, so the annotation is unnecessary. Worse, adding it (and its import) requires theorg.springframework.boot:spring-boot-webtestclientartifact, which may not be on the classpath, breaking compilation.This PR guards the recipe with an additional precondition so the annotation is only added when
WebTestClientis not constructed manually:The check assumes a test either creates all of its
WebTestClientinstances manually or none of them.Why
Reported while validating
UpgradeSpringBoot_4_0on a customer multi-module migration: the recipe added@AutoConfigureWebTestClientto a manually-configured test, introducing aorg.springframework.boot.webtestclient.autoconfigureimport for a package that wasn't available, which broke compilation of an already-SB4 module.moderneinc/customer-requests#2572. It does not add thespring-boot-webtestclientdependency for the cases where the annotation is legitimately added — that remains open.Testing
Added
shouldNotAddAnnotationIfConfiguredManualcovering the manual-build case, and extended theWebTestClienttest stub withbindToServer()/MockServerSpecso the call chain has proper type attribution. All tests inAddAutoConfigureWebTestClientTestpass.