简体   繁体   中英

Multiple Rest calls in Java using RestAssured

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.

RestAssured 只支持并行运行,不支持异步运行。

If I understand your question correctly, you should be able to do what you are planning to do using threads.

   ExecutorService executorService = Executors.newFixedThreadPool(<number of threads>);

    // send 1st 7 requests here
    for (int i = 0; i < 7; i++){
        executorService.execute(new Runnable() {
            @Override
            public void run(){
                try {
                    given().
                        contentType(<>).
                        header(<headers>).
                        body(body).
                    when().
                        post(<URL>);
                } catch (Exception e) {
                    LOGGER.error(e);
                }
            }
        });
    }

    // Wait for all the threads to gracefully shutdown
    try {
        executorService.awaitTermination(<Your timeout>);
    } catch (InterruptedException ie) {
        LOGGER.error("Shutdown interrupted. Will try to shutdown again.", ie);
        executorService.shutdownNow();
    }

Though this won't be asynchronous, this will do what you were planning to do.

As you mentioned you are using TestMG so write both request in separate test methods and run test in parallel using XML.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" parallel="methods" >
<test name="testGuru">
<classes>
<class name="Test_class_name">
</class>
</classes>
</test>
</suite>

Completable Future can be used to make calls async.

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM