0

Please find the below base class for API request. Due to static request/response, I am not able to do parallel test execution. My framework is based on Cucumber, Junit, Java, Json based request.

 public class APIreq {
private static Integer statusCode = 0;
private static String statusLine = null;
private static String resource = null;
protected static Response getResponse;
protected static Response postResponse;
public static JSONObject request = null;
public static JSONObject fileRequest = null;

protected static void postRequest(JSONObject requestBody, String endpoint) throws IOException {
    resource1 = endpoint;
    request = requestBody;
    HashMap<String, String> headerMap = new HashMap<>();
    headerMap.put( "Content-Type", "application/json" );
    headerMap.put("Authorization",ABCD);

    try {
        postResponse = RestAssured.given()
                .log()
                .all()
                .headers( headerMap )
                .body( request.toString() )
                .post( Hooks.targetURL + resource1 )
                .then()
                .extract()
                .response();
        postResponse.prettyPrint();
        statusCode = postResponse.getStatusCode();
        statusLine = postResponse.getStatusLine();
    } catch (Exception e) {
        logger.error( "Error: " + e.getMessage() );
    }
}
public static void assertJsonPostReq(String attribute, String expectedValue) {
    io.restassured.path.json.JsonPath jsonPathEvaluator = postResponse.jsonPath();
    String attributeValue = jsonPathEvaluator.get(attribute);
    SYSO( attribute + ": Expected: " + expectedValue + ", Actual: " + attributeValue );
    Assert.assertTrue(attributeValue.contains(expectedValue));
}

}

Anuj Bajpai
  • 13
  • 1
  • 3

1 Answers1

1

Honest opinion

If you are just trying to automate parallel API tests, then best try Karate, might be an overhead, but worth it in the long run

Karate - Parallel Execution process

GitHub link

Example of Karate BDD test case scenarios

Also based that you currently use Cucumber as your test framework, it will be easy for you to pick this up, as Karate uses the BDD syntax. You can also use the Cucumber HTML reporting tool to create your reports.

djmonki
  • 3,020
  • 7
  • 18