Search code examples
patchrest-assured

java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <500>


I am trying to do a PATCH call where i expect to update a record but i am getting 500 error, I checked the endpoint is mentioned correctly.The test code is as mentioned below:

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.http.ContentType;

import java.util.Map;

import static io.restassured.RestAssured.*;

public class Test03_PartialUpdateBooking {
    @Test
    public void PartialUpdateBooking() {
        JSONObject payload = new JSONObject();
        payload.put("firstname","Tiger");
        payload.put("lastname","Test");
        //String AuthToken ="Basic YWRtaW46cGFzc3dvcmQxMjM=";
        
        System.out.println(payload);
        System.out.println(payload.toJSONString());
        
        given().
        header("Authorization", "Basic YWRtaW46cGFzc3dvcmQxMjM=").
        //header("Authorization","AuthToken").
        contentType(ContentType.JSON).
        accept(ContentType.JSON).
        header("cookie","token=abc123").
        body(payload.toJSONString()).
        when().
        patch("https://restful-booker.herokuapp.com/booking/2").
        then().
        statusCode(200).
        log().all();
            
    
    }

}

i am expecting to run the code correctly and return 200 but it is returning the 500 which means it is unable to reach the server itself. The request runs fine in postman.Can someone help here?


Solution

  • Problem is ContentType.JSON here is not equal application/json. It seems a bug to me, but I don't know the reason behind it.

    The fix would be

    contentType("application/json").
    accept("application/json").