how are you?
I'm having trouble running my automated API test suite.
Problem:
When I run the API tests within a JUnit5 suite class, I noticed that with each test run, the Header of my request is duplicated or even tripled. This results in the failure of further tests.
The strange thing is that the tests that fail in this run work normally when run individually.
private static RequestSpecification reqSpec;
private static ResponseSpecification respSpec;
@BeforeAll
public static void setup(){
baseURI = BASE_URI;
port = PORT;
reqSpec = new RequestSpecBuilder()
.log(LogDetail.ALL)
.setContentType(CONTENT_TYPE)
.setAccept("application/json")
.build();
respSpec = new ResponseSpecBuilder()
.expectResponseTime(lessThan(TIMEOUT))
.log(LogDetail.ALL)
.build();
requestSpecification = reqSpec;
responseSpecification = respSpec;
}
In the code above, center the RequestSpecification in a @BeforeAll
for each test case.
package tests.booker;
import core.BaseRequestSpecification;
import entities.PayLoads;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class CreateBooking extends BaseRequestSpecification {
private final String RESOURCE_BOOKER_ID = "booking";
@Test
@DisplayName("Listar IDs de Bookings")
public void listarIDsDeBooking() {
given()
.body(PayLoads.criarNewBooking())
.pathParams("recurso", RESOURCE_BOOKER_ID)
.when()
.post("/{recurso}")
.then()
.statusCode(HttpStatus.SC_OK);
}
}
Request method: POST
Request URI: https://restful-booker.herokuapp.com:443/booking
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: recurso=booking
Headers: Accept=application/json
Content-Type=application/json
Cookies: <none>
Multiparts: <none>
Body:
{
"firstname": "Rafael",
"lastname": "Pereira",
"totalprice": 350,
"depositpaid": true,
"bookingdates": {
"checkin": "2022-01-01",
"checkout": "2022-09-08"
},
"additionalneeds": "Breakfast"
}
Request method: POST
Request URI: https://restful-booker.herokuapp.com:443/booking
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: recurso=booking
Headers: Accept=application/json
Content-Type=application/json
Cookies: <none>
Multiparts: <none>
Body:
{
"firstname": "Rafael",
"lastname": "Pereira",
"totalprice": 350,
"depositpaid": true,
"bookingdates": {
"checkin": "2022-01-01",
"checkout": "2022-09-08"
},
"additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 202
Etag: W/"ca-SeG/svUxAe31ACn85hvsVDB4nWM"
Date: Sun, 18 Dec 2022 03:34:57 GMT
Via: 1.1 vegur
{
"bookingid": 1897,
"booking": {
"firstname": "Rafael",
"lastname": "Pereira",
"totalprice": 350,
"depositpaid": true,
"bookingdates": {
"checkin": "2022-01-01",
"checkout": "2022-09-08"
},
"additionalneeds": "Breakfast"
}
}
looking at the console output, they noticed that the API header is assembled a second time. this occurs with the other tests in the suite.
A similar problem: Duplicate headers are added at runtime in Rest assured