Skip to content
Latchkey

Gatling "j.n.ConnectException: Connection refused" in CI

Gatling reports every request as KO with "j.n.ConnectException: Connection refused" when the target host actively refuses the TCP connection. In CI this means the service under test was not up, or the baseUrl points somewhere nothing is listening.

What this error means

The run shows 100% KO and the error table is dominated by "j.n.ConnectException: Connection refused: localhost/127.0.0.1:8080". No successful requests are recorded.

Gatling
---- Errors --------------------------------------------------------------------
> j.n.ConnectException: Connection refused: localhost/127.0.0.1:8080    5000 (100.0%)

Common causes

The service under test is not listening

Gatling started before the app or container began accepting connections, so every request is refused.

The baseUrl points at the wrong host or port

A baseUrl of localhost when the app runs in a service container, or a port that is not published, means nothing accepts the connection.

How to fix it

Wait for readiness before Gatling runs

  1. Poll the target health endpoint until it responds.
  2. Run the Gatling simulation only after the target is confirmed up.
  3. Set the baseUrl to the address that is actually reachable in CI.
.github/workflows/ci.yml
- run: |
    until curl -sf http://localhost:8080/health; do sleep 2; done
- run: ./mvnw gatling:test -DbaseUrl=http://localhost:8080

Set baseUrl from the reachable address

Pass the correct host and port into the simulation so it targets what CI publishes.

LoadSimulation.scala
val httpProtocol = http.baseUrl(System.getProperty("baseUrl", "http://localhost:8080"))

How to prevent it

  • Wait for the target to be ready before launching Gatling.
  • Parameterize baseUrl and pass the reachable CI address.
  • Publish the app port the simulation targets in CI.

Frequently asked questions

What causes ""j.n.ConnectException: Connection refused""?
Gatling started before the app or container began accepting connections, so every request is refused.
How do I fix "j.n.ConnectException: Connection refused"?
Wait for readiness before Gatling runs

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card