java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <500> – in restassured -Post endpoint test

huangapple go评论110阅读模式
英文:

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.

以下是要翻译的代码部分:

  1. import static io.restassured.RestAssured.*;
  2. import java.util.HashMap;
  3. import org.json.simple.JSONObject;
  4. import org.testng.annotations.BeforeClass;
  5. import org.testng.annotations.Test;
  6. import io.restassured.RestAssured;
  7. import io.restassured.http.ContentType;
  8. public class Test01_CreateBooking {
  9. @Test
  10. public void CreateBooking() {
  11. JSONObject request = new JSONObject();
  12. request.put("firstname", "Tom");
  13. request.put("lastname", "Payconiq");
  14. request.put("totalprice", "677");
  15. request.put("depositpaid", "true");
  16. request.put("bookingdates.checkin", "2023-06-06");
  17. request.put("bookingdates.checkout", "2023-07-07");
  18. request.put("additionalneeds", "Breakfast");
  19. System.out.println(request.toJSONString());
  20. //baseURI = "https://restful-booker.herokuapp.com/booking";
  21. given().
  22. header("Content-Type", "application/json").
  23. contentType(ContentType.JSON).
  24. accept(ContentType.JSON).
  25. body(request.toJSONString()).
  26. when().
  27. post("https://restful-booker.herokuapp.com/booking").
  28. then().
  29. statusCode(200)
  30. .log().all();
  31. }
  32. }

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:

  1. import static io.restassured.RestAssured.*;
  2. import java.util.HashMap;
  3. import org.json.simple.JSONObject;
  4. import org.testng.annotations.BeforeClass;
  5. import org.testng.annotations.Test;
  6. import io.restassured.RestAssured;
  7. import io.restassured.http.ContentType;
  8. public class Test01_CreateBooking {
  9. @Test
  10. public void CreateBooking() {
  11. JSONObject request = new JSONObject();
  12. request.put(&quot;firstname&quot;, &quot;Tom&quot;);
  13. request.put(&quot;lastname&quot;, &quot;Payconiq&quot;);
  14. request.put(&quot;totalprice&quot;, &quot;677&quot;);
  15. request.put(&quot;depositpaid&quot;, &quot;true&quot;);
  16. request.put(&quot;bookingdates.checkin&quot;, &quot;2023-06-06&quot;);
  17. request.put(&quot;bookingdates.checkout&quot;, &quot;2023-07-07&quot;);
  18. request.put(&quot;additionalneeds&quot;, &quot;Breakfast&quot;);
  19. System.out.println(request.toJSONString());
  20. //baseURI = &quot;https://restful-booker.herokuapp.com/booking&quot;;
  21. given().
  22. header(&quot;Content-Type&quot;, &quot;application/json&quot;).
  23. contentType(ContentType.JSON).
  24. accept(ContentType.JSON).
  25. body(request.toJSONString()).
  26. when().
  27. post(&quot;https://restful-booker.herokuapp.com/booking&quot;).
  28. then().
  29. statusCode(200)
  30. .log().all();
  31. }
  32. }

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 {

  1. @Test
  2. public void CreateBooking() {
  3. JSONObject request = new JSONObject();
  4. request.put(&quot;firstname&quot;, &quot;Tom&quot;);
  5. request.put(&quot;lastname&quot;, &quot;Payconiq&quot;);
  6. request.put(&quot;totalprice&quot;, &quot;677&quot;);
  7. request.put(&quot;depositpaid&quot;, &quot;true&quot;);
  8. JSONObject bookingDates = new JSONObject();
  9. bookingDates.put(&quot;checkin&quot;, &quot;2023-06-03&quot;);
  10. bookingDates.put(&quot;checkout&quot;, &quot;2023-07-05&quot;);
  11. request.put(&quot;bookingdates&quot;, bookingDates);
  12. request.put(&quot;additionalneeds&quot;, &quot;Breakfast&quot;);
  13. System.out.println(request.toJSONString());
  14. //baseURI = &quot;https://restful-booker.herokuapp.com/booking&quot;;
  15. given().contentType(ContentType.JSON) .body(request.toJSONString()).when()
  16. .post(&quot;https://restful-booker.herokuapp.com/booking&quot;).then().statusCode(200).log()
  17. .all();
  18. }

}

英文:

Its because the json body you are sending is invalid , you are expecting json simple

  1. bookingdates.checkin

to convert to below nested key which won't work, it will treat it as a key only

  1. &quot;bookingdates&quot;: {
  2. &quot;checkin&quot;: &quot;&quot;
  3. }

What you are sending is this

  1. {
  2. &quot;firstname&quot;: &quot;Tom&quot;,
  3. &quot;additionalneeds&quot;: &quot;Breakfast&quot;,
  4. &quot;bookingdates.checkin&quot;: &quot;2023-06-06&quot;,
  5. &quot;bookingdates.checkout&quot;: &quot;2023-07-07&quot;,
  6. &quot;totalprice&quot;: &quot;677&quot;,
  7. &quot;depositpaid&quot;: &quot;true&quot;,
  8. &quot;lastname&quot;: &quot;Payconiq&quot;
  9. }

and correct way is this

  1. {
  2. &quot;firstname&quot;: &quot;Tom&quot;,
  3. &quot;additionalneeds&quot;: &quot;Breakfast&quot;,
  4. &quot;bookingdates&quot;: {
  5. &quot;checkin&quot;: &quot;2023-06-06&quot;,
  6. &quot;checkout&quot;: &quot;2023-07-07&quot;
  7. },
  8. &quot;totalprice&quot;: &quot;677&quot;,
  9. &quot;depositpaid&quot;: &quot;true&quot;,
  10. &quot;lastname&quot;: &quot;Payconiq&quot;
  11. }

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

  1. import static io.restassured.RestAssured.given;
  2. import io.restassured.http.ContentType;
  3. import org.json.simple.JSONObject;
  4. import org.junit.Test;
  5. public class Test01_CreateBooking {
  6. @Test
  7. public void CreateBooking() {
  8. JSONObject request = new JSONObject();
  9. request.put(&quot;firstname&quot;, &quot;Tom&quot;);
  10. request.put(&quot;lastname&quot;, &quot;Payconiq&quot;);
  11. request.put(&quot;totalprice&quot;, &quot;677&quot;);
  12. request.put(&quot;depositpaid&quot;, &quot;true&quot;);
  13. JSONObject bookingDates = new JSONObject();
  14. bookingDates.put(&quot;checkin&quot;, &quot;2023-06-03&quot;);
  15. bookingDates.put(&quot;checkout&quot;, &quot;2023-07-05&quot;);
  16. request.put(&quot;bookingdates&quot;, bookingDates);
  17. request.put(&quot;additionalneeds&quot;, &quot;Breakfast&quot;);
  18. System.out.println(request.toJSONString());
  19. //baseURI = &quot;https://restful-booker.herokuapp.com/booking&quot;;
  20. given().contentType(ContentType.JSON) .body(request.toJSONString()).when()
  21. .post(&quot;https://restful-booker.herokuapp.com/booking&quot;).then().statusCode(200).log()
  22. .all();
  23. }
  24. }

huangapple
  • 本文由 发表于 2023年6月8日 15:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定