I am struggling to write a proper Integration Test for my application. I use rest assured and also maven failsafe plugin.
Currently I get an exception like this:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost/api/deactivate/1": Connect to localhost:80 [localhost/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:80 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
My assumption is that there is a problem with missing port (8080) in url. However I do not know why.
I have two modules, one is calling another. The first one is running on port 8081 and the second one 8080.
This is test config for module 1 (module 2 config is similar but another port). My tests extend this class:
public abstract class AbstractDeactivationIT {
@BeforeAll
public static void configureRestAssured() {
RestAssured.port = Integer.parseInt(System.getProperty("it.deactivation.port", "8081"));
System.out.println("RestAssured: using port " + RestAssured.port);
// authentication config
...
var mapper = ObjectMapperFactory.defaultConfig().build();
RestAssured.config = RestAssured.config()
.logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails())
.objectMapperConfig(objectMapperConfig().jackson2ObjectMapperFactory((type, s) -> mapper));
}
}
My test:
@Test
void testDeactivation_forCorrectRequestData() {
// @formatter:off
given()
.contentType(JSON)
.body(DeactivationRequest.builder()
...
.build()
).
when()
.post("/api/deactivations").
then()
.statusCode(201);
// @formatter:on
}
While debugging I noticed that first call is build correctly (with port 8081), but the client call is without 8080 port. I have both urls with ports in my application-local.yml files. I also have similar test but in the opposite direction, so module 2 is calling module 1 and that works fine without any port problems. Urls are built correctly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…