Skip to content

Skip @AutoConfigureWebTestClient when WebTestClient is built manually#1040

Merged
MBoegers merged 1 commit into
mainfrom
skip-autoconfigure-webtestclient-when-built-manually
Jun 17, 2026
Merged

Skip @AutoConfigureWebTestClient when WebTestClient is built manually#1040
MBoegers merged 1 commit into
mainfrom
skip-autoconfigure-webtestclient-when-built-manually

Conversation

@MBoegers

@MBoegers MBoegers commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

What's changed

AddAutoConfigureWebTestClient adds @AutoConfigureWebTestClient to @SpringBootTest tests that reference WebTestClient. However, when a test builds its WebTestClient manually — e.g. WebTestClient.bindToServer().build() — the bean is not auto-configured, so the annotation is unnecessary. Worse, adding it (and its import) requires the org.springframework.boot:spring-boot-webtestclient artifact, 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 WebTestClient is not constructed manually:

not(new UsesMethod<>("org.springframework.test.web.reactive.server.WebTestClient.MockServerSpec build()", true))

The check assumes a test either creates all of its WebTestClient instances manually or none of them.

Why

Reported while validating UpgradeSpringBoot_4_0 on a customer multi-module migration: the recipe added @AutoConfigureWebTestClient to a manually-configured test, introducing a org.springframework.boot.webtestclient.autoconfigure import for a package that wasn't available, which broke compilation of an already-SB4 module.

  • This partially addresses the WebTestClient gap (Gap 2) from moderneinc/customer-requests#2572. It does not add the spring-boot-webtestclient dependency for the cases where the annotation is legitimately added — that remains open.

Testing

Added shouldNotAddAnnotationIfConfiguredManual covering the manual-build case, and extended the WebTestClient test stub with bindToServer() / MockServerSpec so the call chain has proper type attribution. All tests in AddAutoConfigureWebTestClientTest pass.

  • Partially fixes moderneinc/customer-requests#2572 — addresses Gaps 1 and 2.

…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).
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 17, 2026
@MBoegers MBoegers merged commit 1d13bf6 into main Jun 17, 2026
1 check passed
@MBoegers MBoegers deleted the skip-autoconfigure-webtestclient-when-built-manually branch June 17, 2026 08:05
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants