英文:
java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <500> - in restassured -Post endpoint test
问题
I am new to RestAssured, trying to create a test for POST endpoint but it returns 500 when I am executing it. Not sure what the problem is.
以下是要翻译的代码部分:
import static io.restassured.RestAssured.*;
import java.util.HashMap;
import org.json.simple.JSONObject;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class Test01_CreateBooking {
@Test
public void CreateBooking() {
JSONObject request = new JSONObject();
request.put("firstname", "Tom");
request.put("lastname", "Payconiq");
request.put("totalprice", "677");
request.put("depositpaid", "true");
request.put("bookingdates.checkin", "2023-06-06");
request.put("bookingdates.checkout", "2023-07-07");
request.put("additionalneeds", "Breakfast");
System.out.println(request.toJSONString());
//baseURI = "https://restful-booker.herokuapp.com/booking";
given().
header("Content-Type", "application/json").
contentType(ContentType.JSON).
accept(ContentType.JSON).
body(request.toJSONString()).
when().
post("https://restful-booker.herokuapp.com/booking").
then().
statusCode(200)
.log().all();
}
}
I am expecting a 200 and the record should be created but it returns 500. Can someone please help.
英文:
I am new to RestAssured, trying to create a test for POST endpoint but it returns 500 when i am executing it. Not sure what the problem is.
Below mentioned is the code:
import static io.restassured.RestAssured.*;
import java.util.HashMap;
import org.json.simple.JSONObject;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class Test01_CreateBooking {
@Test
public void CreateBooking() {
JSONObject request = new JSONObject();
request.put("firstname", "Tom");
request.put("lastname", "Payconiq");
request.put("totalprice", "677");
request.put("depositpaid", "true");
request.put("bookingdates.checkin", "2023-06-06");
request.put("bookingdates.checkout", "2023-07-07");
request.put("additionalneeds", "Breakfast");
System.out.println(request.toJSONString());
//baseURI = "https://restful-booker.herokuapp.com/booking";
given().
header("Content-Type", "application/json").
contentType(ContentType.JSON).
accept(ContentType.JSON).
body(request.toJSONString()).
when().
post("https://restful-booker.herokuapp.com/booking").
then().
statusCode(200)
.log().all();
}
}
I am expecting a 200 and the record should be created but it retuns 500.Can someone please help.
答案1
得分: 0
由于您发送的 JSON 主体无效,您期望将“bookingdates.checkin”转换为以下嵌套键,但这样做不起作用,它将把它视为一个键:"bookingdates": {"checkin": ""}
您发送的内容是这样的:
{
"firstname": "Tom",
"additionalneeds": "Breakfast",
"bookingdates.checkin": "2023-06-06",
"bookingdates.checkout": "2023-07-07",
"totalprice": "677",
"depositpaid": "true",
"lastname": "Payconiq"
}
正确的方式是这样的:
{
"firstname": "Tom",
"additionalneeds": "Breakfast",
"bookingdates": {
"checkin": "2023-06-06",
"checkout": "2023-07-07"
},
"totalprice": "677",
"depositpaid": "true",
"lastname": "Payconiq"
}
您需要创建一个 bookingdates 对象,然后在其下添加更多键以获得正确的 JSON 结构。
使用以下代码,应该能正常工作:
import static io.restassured.RestAssured.given;
import io.restassured.http.ContentType;
import org.json.simple.JSONObject;
import org.junit.Test;
public class Test01_CreateBooking {
@Test
public void CreateBooking() {
JSONObject request = new JSONObject();
request.put("firstname", "Tom");
request.put("lastname", "Payconiq");
request.put("totalprice", "677");
request.put("depositpaid", "true");
JSONObject bookingDates = new JSONObject();
bookingDates.put("checkin", "2023-06-03");
bookingDates.put("checkout", "2023-07-05");
request.put("bookingdates", bookingDates);
request.put("additionalneeds", "Breakfast");
System.out.println(request.toJSONString());
//baseURI = "https://restful-booker.herokuapp.com/booking";
given().contentType(ContentType.JSON) .body(request.toJSONString()).when()
.post("https://restful-booker.herokuapp.com/booking").then().statusCode(200).log()
.all();
}
}
英文:
Its because the json body you are sending is invalid , you are expecting json simple
bookingdates.checkin
to convert to below nested key which won't work, it will treat it as a key only
"bookingdates": {
"checkin": ""
}
What you are sending is this
{
"firstname": "Tom",
"additionalneeds": "Breakfast",
"bookingdates.checkin": "2023-06-06",
"bookingdates.checkout": "2023-07-07",
"totalprice": "677",
"depositpaid": "true",
"lastname": "Payconiq"
}
and correct way is this
{
"firstname": "Tom",
"additionalneeds": "Breakfast",
"bookingdates": {
"checkin": "2023-06-06",
"checkout": "2023-07-07"
},
"totalprice": "677",
"depositpaid": "true",
"lastname": "Payconiq"
}
You need to created a bookingdates object and then add more keys under it to get proper structure of json
use below code , should work fine
import static io.restassured.RestAssured.given;
import io.restassured.http.ContentType;
import org.json.simple.JSONObject;
import org.junit.Test;
public class Test01_CreateBooking {
@Test
public void CreateBooking() {
JSONObject request = new JSONObject();
request.put("firstname", "Tom");
request.put("lastname", "Payconiq");
request.put("totalprice", "677");
request.put("depositpaid", "true");
JSONObject bookingDates = new JSONObject();
bookingDates.put("checkin", "2023-06-03");
bookingDates.put("checkout", "2023-07-05");
request.put("bookingdates", bookingDates);
request.put("additionalneeds", "Breakfast");
System.out.println(request.toJSONString());
//baseURI = "https://restful-booker.herokuapp.com/booking";
given().contentType(ContentType.JSON) .body(request.toJSONString()).when()
.post("https://restful-booker.herokuapp.com/booking").then().statusCode(200).log()
.all();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论