Skip to content
Latchkey

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 refused

Common 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

  1. Start the application, then wait for a health endpoint to respond.
  2. Only run the REST Assured module once the endpoint is reachable.
  3. Confirm baseURI and port match the running app.
Terminal
until curl -sf http://localhost:8080/actuator/health; do sleep 2; done
mvn -q test

Set 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →