Repository navigation
fix: only reset default WireMock client for tests that opt in (fixes #210) - #211
Merged
tomasbjerre merged 3 commits intoSep 21, 2026
Conversation
…210) 4.4.0 moved resetting the static WireMock DSL client from WireMockSpringJunitExtension into WireMockTestExecutionListener, but the two have very different scope: the extension is registered by @ExtendWith on @EnableWireMock/@ConfigureWireMock, whereas the listener is registered in META-INF/spring.factories and so runs for every Spring test in any project that merely has wiremock-spring-boot on its test classpath. afterTestMethod called WireMock.configureFor(-1) unconditionally, which for a test that never opted in clobbers whatever else configured the static client - notably WireMock's own @wiremocktest extension, which configures it once per test class. Every test method after the first then addressed localhost with port -1, rendered as the URI default: org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:80 failed: Connection refused beforeTestMethod and afterTestMethod now return early unless the test class declares @EnableWireMock or @ConfigureWireMock, restoring the pre-4.4.0 scope. prepareTestInstance stays ungated: it is already a no-op without @InjectWireMock fields, and gating it would turn a missing @EnableWireMock from a clear IllegalStateException into a silently unset field. Adds usecases.CoreWireMockTestAnnotationTest, a @SpringBootTest using @wiremocktest that stubs with the static DSL in two ordered test methods. It fails on main with exactly the reported error and passes with this change. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Adds corewiremock.GetNumberAdapterTest, the scenario from the issue itself: a @SpringBootTest whose service bean talks to a statically configured port via RestClient, stubbed with the static WireMock DSL through WireMock's own @wiremocktest. Without the preceding fix its second test method fails with the reported error, org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:80 failed: Connection refused This needs RestClient.Builder to be auto-configured, which in Spring Boot 4 lives in spring-boot-starter-restclient rather than spring-boot-starter-web, so that starter is added as a test dependency. The adapter bean and its @SpringBootApplication live in their own top-level package, so that usecases.App's component scan over the "usecases" package does not sweep the bean into every other test's application context - the same reason nodirtycontext has its own app. CoreWireMockTestAnnotationTest moves there too, keeping both tests for this regression together: that one covers the same defect with a dynamic port and no service bean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
… scaffolding The RestClient in the issue was incidental: the call that fails is stubFor, WireMock's own admin call over the static DSL client, not the application's request. So the service bean added nothing to the reproduction, and everything propping it up went with it - the @SpringBootApplication that existed only to keep that @service out of usecases.App's component scan, and the spring-boot-starter-restclient test dependency that RestClient.Builder injection needed. The test is now a single class in usecases that stubs and asserts with rest-assured, like the rest of the suite, keeping the issue's fixed port and its status404/statusOk methods. It still fails without the preceding fix with the reported error: org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:80 failed: Connection refused Also drops the duplicate rest-assured testImplementation line. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Fixes #210.
The regression
4.4.0 moved per-test WireMock behavior out of
WireMockSpringJunitExtension(a JUnit Jupiter extension) and intoWireMockTestExecutionListener, so that it also works under Cucumber, TestNG and JUnit 4 (#198).Those two are not registered the same way, though:
WireMockSpringJunitExtensionis registered by@ExtendWithon@EnableWireMock/@ConfigureWireMock, so it only ever ran for tests that opted in.WireMockTestExecutionListeneris registered inMETA-INF/spring.factories, so Spring runs it for every test in a project that merely has wiremock-spring-boot on its test classpath.afterTestMethodreset the static WireMock DSL client unconditionally:For a Spring test that does not use wiremock-spring-boot at all, that clobbers whatever configured the static client instead - for example WireMock's own
@WireMockTestextension, which configures it once per test class. Every test method after the first then addressedlocalhostwith port-1, which renders as the URI default:The fix
beforeTestMethod/afterTestMethodnow return early unless the test class actually declares@EnableWireMockor@ConfigureWireMock, restoring the pre-4.4.0 scope of the reset. Tests that do not opt in are left entirely alone.prepareTestInstanceis deliberately left ungated: it is already a no-op for classes without@InjectWireMockfields, and gating it would turn a missing@EnableWireMockfrom a clearIllegalStateExceptioninto a silently unset field.Test
usecases.CoreWireMockTestAnnotationTestis the reproduction from the issue: a@SpringBootTestthat does not opt in to wiremock-spring-boot, stubbing with the static DSL under@WireMockTest(httpPort = 8146), with the issue'sstatus404andstatusOkmethods.The issue's
RestClientservice bean is deliberately not reproduced. The call that fails isstubFor- WireMock's own admin call over the static DSL client - not the application's request, which is why the reported stack trace is Apache HttpClient and never reachesRestClient. So the test stubs and asserts with rest-assured like the rest of the suite, needing no service bean, no extra@SpringBootApplicationand no new dependency.The method order is fixed because only a test method that is not the first one fails:
@WireMockTestconfigures the static client once per test class, and the first method'safterTestMethodis what destroyed it.On
mainit fails with exactly the reported error:With this change
./gradlew buildis green: 79 tests, 0 failures.Unrelated tidy-up
build.gradledeclaredio.rest-assured:rest-assured:6.0.0as atestImplementationtwice; one line is removed.🤖 Generated with Claude Code