Cucumber Java集成WireMock与RestAssured存根初始化报错排查
Let's walk through why you're seeing that Connection refused: connect error pointing to localhost:8080 (even though you configured WireMock for port 8089) and get your stubbing working properly.
Root Causes to Check
First, let's pinpoint the most likely issues:
- RestAssured is targeting the wrong port: By default, RestAssured sends requests to
localhost:8080, but your WireMock instance is running on 8089. Mismatched ports = connection failure. - Cucumber + JUnit Rule lifecycle conflict: JUnit's
@Ruleis managed by JUnit's lifecycle, but Cucumber has its own execution flow. If your step runs before theWireMockRuleinitializes, WireMock won't be up yet. - Outdated dependency versions: JUnit 4.10 is pretty old, and it might have compatibility issues with WireMock 2.26.0.
Solution 1: Fix JUnit 4 + WireMockRule Setup
If you want to stick with JUnit 4, here's how to fix things:
1. Point RestAssured to WireMock's Port
Add a Cucumber @Before hook to set RestAssured's base URI and port to match your WireMock configuration. This ensures all your API requests go to the right place:
import io.cucumber.java.Before; import static io.restassured.RestAssured.*; public class YourStepDefinitions { protected final int PORT = 8089; @Rule public WireMockRule wiremockRule = new WireMockRule(PORT); @Before public void configureRestAssured() { baseURI = "http://localhost"; port = PORT; } @Given("^mock environment has been setup$") public void mockEnvironmentHasBeenSetup() { // Verify WireMock is running by printing the port System.out.println("WireMock active on port: " + wiremockRule.port()); stubFor(any(anyUrl()).willReturn(ok())); } }
2. Update Dependencies for Compatibility
Upgrade JUnit 4 to a newer stable version (4.13.2) and add the cucumber-junit dependency to ensure Cucumber recognizes JUnit rules:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>6.11.0</version> <!-- Match your Cucumber core version --> <scope>test</scope> </dependency>
3. Verify WireMock is Running
Test if WireMock is up by visiting http://localhost:8089/__admin in your browser. If you see WireMock's admin interface, the service is running correctly.
Solution 2: Switch to JUnit 5 + WireMockServer (Better for Modern Setups)
If you want to use JUnit 5, skip manual server management and use WireMock's official JUnit 5 extension:
1. Update Dependencies
Use WireMock's JUnit 5 support and modernize your test dependencies:
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-jre8</artifactId> <version>2.35.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-jre8-junit5</artifactId> <version>2.35.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit-platform-engine</artifactId> <version>7.11.0</version> <!-- Match your Cucumber version --> <scope>test</scope> </dependency>
2. Use WireMockExtension for Lifecycle Management
Let the extension handle starting/stopping WireMock, and configure RestAssured correctly:
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import io.cucumber.java.Before; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static io.restassured.RestAssured.*; public class YourStepDefinitions { @RegisterExtension static WireMockExtension wireMockServer = WireMockExtension.newInstance() .options(wireMockConfig().port(8089)) .build(); @Before public void configureRestAssured() { baseURI = "http://localhost"; port = wireMockServer.getPort(); } @Given("^mock environment has been setup$") public void mockEnvironmentHasBeenSetup() { wireMockServer.stubFor(any(anyUrl()).willReturn(ok())); System.out.println("WireMock active on port: " + wireMockServer.getPort()); } }
3. Avoid Manual Server Management
If you insist on manual WireMockServer setup, add an @After hook to stop the server after tests to prevent port leaks:
@After public void teardownWireMock() { wireMockServer.stop(); }
Quick Troubleshooting Checklist
- Double-check RestAssured's
portsetting matches WireMock's port. This is the #1 cause of this error. - Confirm WireMock is running by accessing the admin endpoint (
/__admin). - Ensure dependencies are compatible (no mismatched JUnit/WireMock versions).
内容的提问来源于stack exchange,提问作者Shabar

