I am trying to convert string to JSONObject. This is my code:
JSONObject obj = new JSONObject(str);
Vehicle.feature file contains :
Scenario: Create a vehicle with valid json request
Given vehicle json for VehicleService
"""
"{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}"
"""
When performing POST on VehicleService url /add
Then VehicleService should return status code 200
VehicleStepDefs contains:
@Given("^vehicle json for VehicleService$")
public void submitValidVehicleRequest(String vehicleJson) throws JSONException {
JSONObject obj = new JSONObject(vehicleJson);
request = given().and()
.header("Content-Type", MediaType.APPLICATION_JSON)
.accept(ContentType.JSON)
.body(obj);
request.then().log().all();
}
My error looks like this :
org.json.JSONException: Value {"vin" : "VIN5", "brand" : "Toyota", "model" : "Innova", "year" : "2017", "color" : "Red", "modelCode" : "1234", "type" : "M", "countryCode" : "JP", "isConnected" : "true", "isActive" : "true"} of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:159)
at org.json.JSONObject.<init>(JSONObject.java:172)
at com.examples.demo.VehicleStepDefs.submitValidVehicleRequest(VehicleStepDefs.java:43)
at ?.Given vehicle json for VehicleService(Vehicle.feature:8)
What am I doing wrong?