Skip to content

fix: only reset default WireMock client for tests that opt in (fixes #210) - #211

Merged
tomasbjerre merged 3 commits into
mainfrom
fix/210-do-not-reset-default-wiremock-client-for-unrelated-tests
Sep 21, 2026
Merged

tomasbjerre merged 3 commits into
mainfrom
fix/210-do-not-reset-default-wiremock-client-for-unrelated-tests

Conversation

@tomasbjerre

@tomasbjerre tomasbjerre commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #210.

The regression

4.4.0 moved per-test WireMock behavior out of WireMockSpringJunitExtension (a JUnit Jupiter extension) and into WireMockTestExecutionListener, so that it also works under Cucumber, TestNG and JUnit 4 (#198).

Those two are not registered the same way, though:

  • WireMockSpringJunitExtension is registered by @ExtendWith on @EnableWireMock/@ConfigureWireMock, so it only ever ran for tests that opted in.
  • WireMockTestExecutionListener is registered in META-INF/spring.factories, so Spring runs it for every test in a project that merely has wiremock-spring-boot on its test classpath.

afterTestMethod reset the static WireMock DSL client unconditionally:

@Override
public void afterTestMethod(final TestContext testContext) {
  WireMock.configureFor(-1);
}

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 @WireMockTest extension, which configures it once per test class. Every test method after the first then addressed localhost with port -1, which renders as the URI default:

org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:80 failed: Connection refused

The fix

beforeTestMethod/afterTestMethod now return early unless the test class actually declares @EnableWireMock or @ConfigureWireMock, restoring the pre-4.4.0 scope of the reset. Tests that do not opt in are left entirely alone.

prepareTestInstance is deliberately left ungated: it is already a no-op for classes without @InjectWireMock fields, and gating it would turn a missing @EnableWireMock from a clear IllegalStateException into a silently unset field.

Test

usecases.CoreWireMockTestAnnotationTest is the reproduction from the issue: a @SpringBootTest that does not opt in to wiremock-spring-boot, stubbing with the static DSL under @WireMockTest(httpPort = 8146), with the issue's status404 and statusOk methods.

The issue's RestClient service bean is deliberately not reproduced. The call that fails is stubFor - 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 reaches RestClient. So the test stubs and asserts with rest-assured like the rest of the suite, needing no service bean, no extra @SpringBootApplication and no new dependency.

The method order is fixed because only a test method that is not the first one fails: @WireMockTest configures the static client once per test class, and the first method's afterTestMethod is what destroyed it.

On main it fails with exactly the reported error:

CoreWireMockTestAnnotationTest > statusOk() FAILED
    org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:80 failed: Connection refused

With this change ./gradlew build is green: 79 tests, 0 failures.

Unrelated tidy-up

build.gradle declared io.rest-assured:rest-assured:6.0.0 as a testImplementation twice; one line is removed.

🤖 Generated with Claude Code

…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>
@tomasbjerre tomasbjerre added the bug Something isn't working label Sep 21, 2026
tomasbjerre and others added 2 commits September 21, 2026 10:28
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>
@tomasbjerre
tomasbjerre merged commit d1c800b into main Sep 21, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Connect to http://localhost:80 failed: Connection refused

1 participant