REST Assured "java.net.ConnectException: Connection refused" in CI
REST Assured attempted to open a TCP connection to the API and the host actively refused it. In CI this almost always means the service was not up yet, or the base URI points at the wrong host or port.
What this error means
A REST Assured test fails with "java.net.ConnectException: Connection refused (Connection refused)" wrapped in the client stack, before any status assertion runs.
REST Assured
java.net.ConnectException: Connection refused (Connection refused)
at io.restassured.internal.RequestSpecificationImpl...
Caused by: java.net.ConnectException: Connection refusedCommon causes
The API server was not started or not ready
The test suite runs before the application under test binds its port, so the connection is refused.
Wrong baseURI, port, or basePath
RestAssured.baseURI or the request points at a host or port the app does not listen on in CI.
How to fix it
Gate the tests on service readiness
- Start the application, then wait for a health endpoint to respond.
- Only run the REST Assured module once the endpoint is reachable.
- Confirm baseURI and port match the running app.
Terminal
until curl -sf http://localhost:8080/actuator/health; do sleep 2; done
mvn -q testSet the base URI to the CI service
Point REST Assured at the exact host and port the app binds in CI rather than a hardcoded local default.
Java
RestAssured.baseURI = System.getenv().getOrDefault("API_URL", "http://localhost:8080");How to prevent it
- Wait on a health check before starting the API test phase.
- Read the base URI from an env var so CI and local differ cleanly.
- Use a service container gated by a health check.
Related guides
REST Assured "Expected status code <200> but was <500>" in CIFix REST Assured "Expected status code <200> but was <500>" in CI - the statusCode() expectation failed becau…
REST Assured Jackson "JsonParseException" on the response body in CIFix REST Assured "com.fasterxml.jackson.core.JsonParseException" in CI - REST Assured tried to parse a non-JS…
Newman "ECONNREFUSED" against localhost before the server is up in CIFix Newman "ECONNREFUSED" in CI - Newman ran against localhost before the API server had finished starting, s…