Home > OS >  Using Rest Assured to test vs using Rest Assured & TestNG. Can someone explain the difference betwee
Using Rest Assured to test vs using Rest Assured & TestNG. Can someone explain the difference betwee

Time:05-12

They both appear to be effectively doing the same thing. I messed around with it and used different values, and I was getting pass/fail correctly for both.

  @Test
    public static void test_get(){
        RequestSpecification rqs =  new RequestSpecBuilder()
                .setBaseUri("http://localhost:8080")
                .setBasePath("/app/")
                .build();

        ResponseSpecification rss = new ResponseSpecBuilder()
                .expectStatusCode(200)
                .build();

        Response response =  given(rqs, rss).request("GET", "videogames");
    }

vs

@Test
    public static void test_get(){
        RequestSpecification rqs =  new RequestSpecBuilder()
                .setBaseUri("http://localhost:8080")
                .setBasePath("/app/")
                .build();

        Response response =  given(rqs).request("GET", "videogames");

        assertEquals(response.getStatusCode(), 200);
    }

CodePudding user response:

There are a few differences between the two which become more obvious when you work with Rest Assured's built in assertions for a while.

Need to check the response body quickly and clearly? There are Hamcrest matchers for that.

Keeping the request and its assertions together in one big method chain also helps organise your code and makes reading requests back easier.

Rest Assured allows you to lay your tests out like this:

RestAssured.given().contentType(ContentType.JSON)
               .when().get(endpoint)
               .then().body("firstname",containsString("Mark"));

This allows you to read the entire request and assertion life-cycle very cleanly. Make the request, do the assertions. There's little chance for confusion about what's going on.

If you use Rest Assured you should be using its way of doing assertions, in my opinion. It's a tool specialised for working with API requests and responses, in a way that something like TestNG just isn't.

Code snippet taken from: [https://qaautomation.expert/2021/06/14/assertion-of-json-in-rest-assured-using-hamcrest][1]

  • Related