Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

How to set port for client call in Spring boot Integration Test using RestAssured?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The RestAssured.port is a static field. If you run both tests in the same failsafe configuration then the order of the tests can mess with the static attribute.

Do not use the static RestAssured config but construct the proper url with port for every RestAssured call.

You can use the get(), post(), etc methods with an url instead of a relative path (e.g.: .when().get("http://myhost.org:80/doSomething");). Source: https://github.com/rest-assured/rest-assured/wiki/Usage#default-values

In your case it could be:

given()
    .contentType(JSON)
    .body(DeactivationRequest.builder()
        ...
        .build()
    ).
when()
    .post("http://localhost:8081/api/deactivations").
then()
    .statusCode(201);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...