Multiple Rest calls in Java using RestAssured

10,448

Completable Future can be used to make calls async.

List<CompletableFuture<Pair<String,String>>> futures =
    Arrays.stream(endpoints)
        .map(endpoint -> restCallAsync(endpoint))
        .collect(Collectors.toList());

List<Pair<String,String>> result =
    futures.stream()
        .map(CompletableFuture::join)
        .collect(Collectors.toList());
CompletableFuture<Pair<String,String>> restCallAsync(String endpoint) {
    CompletableFuture<Pair<String,String>> future = CompleteableFuture.supplyAsync(new Supplier<Pair<String,String>>() {
        @Override
        public Pair<String,String> get() {
            Response response = given()
                .relaxedHTTPSValidation()
                .get(endpoint)
                .then()
                .extract().response();
            LOG.info("Endpoint : " + endpoint + " Status : " + response.getStatusCode());
            ...
        }
    });
}
Share:
10,448
Admin
Author by

Admin

Updated on November 29, 2022

Comments

  • Admin
    Admin over 1 year

    When you make a rest request using RestAssured, it appears to wait for a response. I need to make a POST request in RestAssured and then while it waits for a response, I need to make a GET request. I'm using Java and RestAssured. I've also tried creating a second thread and it still has not worked. This is for testing purposes.

    Here's where it waits:

    given().auth().preemptive().basic(userCreds[0], userCreds[1]).contentType("application/json;").and().post(baseURL + resourcePath + this.act.getId() + "/run");
    

    I would like this to run while the previous request is running as well (asynchronous request?):

    given().auth().preemptive().basic(userCreds[0], userCreds[1]).when().get(baseURL + resourcePath + this.connect.getId() + "/outgoing/" + fileId);
    

    I've also read that RestAssured supports asynchronous requests, but I've been unsuccessful to get it to work. My project is mavenized. I'm just a lowly QA guy so any help would be greatly appreciated.

  • Vitali Plagov
    Vitali Plagov over 5 years
    Do I have to use the same number of threads in method newFixedThreadPool() and in for-loop?
  • avp
    avp over 5 years
    @VitaliiPlagov No, you need not. They can be different based on your use case of whether you want to run it in caller's thread or a new thread.
  • Amrit
    Amrit over 3 years
    Hi @TT, This is code snapshot for my private repository.
  • TT.
    TT. over 3 years
    Hi Amrit. Better to post the code as text instead a picture. Pictures cannot be searched and indexed from search engines.
  • Amrit
    Amrit over 3 years
    Sure @TT will keep this in mind going forward, but this isn't copied from anywhere.