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

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

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(&quot;firstname&quot;, &quot;Tom&quot;);
	request.put(&quot;lastname&quot;, &quot;Payconiq&quot;);
	request.put(&quot;totalprice&quot;, &quot;677&quot;);
	request.put(&quot;depositpaid&quot;, &quot;true&quot;);
	request.put(&quot;bookingdates.checkin&quot;, &quot;2023-06-06&quot;);
	request.put(&quot;bookingdates.checkout&quot;, &quot;2023-07-07&quot;);
	request.put(&quot;additionalneeds&quot;, &quot;Breakfast&quot;);
		
	System.out.println(request.toJSONString());
	 //baseURI = &quot;https://restful-booker.herokuapp.com/booking&quot;;
	
	given().
		header(&quot;Content-Type&quot;, &quot;application/json&quot;).
		contentType(ContentType.JSON).
		accept(ContentType.JSON).
		body(request.toJSONString()).
	when().
		post(&quot;https://restful-booker.herokuapp.com/booking&quot;).
	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(&quot;firstname&quot;, &quot;Tom&quot;);
	request.put(&quot;lastname&quot;, &quot;Payconiq&quot;);
	request.put(&quot;totalprice&quot;, &quot;677&quot;);
	request.put(&quot;depositpaid&quot;, &quot;true&quot;);
	JSONObject bookingDates = new JSONObject();
	bookingDates.put(&quot;checkin&quot;, &quot;2023-06-03&quot;);
	bookingDates.put(&quot;checkout&quot;, &quot;2023-07-05&quot;);
	request.put(&quot;bookingdates&quot;, bookingDates);
	request.put(&quot;additionalneeds&quot;, &quot;Breakfast&quot;);

	System.out.println(request.toJSONString());
	//baseURI = &quot;https://restful-booker.herokuapp.com/booking&quot;;

	given().contentType(ContentType.JSON) .body(request.toJSONString()).when()
		.post(&quot;https://restful-booker.herokuapp.com/booking&quot;).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

 &quot;bookingdates&quot;: {
            &quot;checkin&quot;: &quot;&quot;
        }

What you are sending is this

{
	&quot;firstname&quot;: &quot;Tom&quot;,
	&quot;additionalneeds&quot;: &quot;Breakfast&quot;,
	&quot;bookingdates.checkin&quot;: &quot;2023-06-06&quot;,
	&quot;bookingdates.checkout&quot;: &quot;2023-07-07&quot;,
	&quot;totalprice&quot;: &quot;677&quot;,
	&quot;depositpaid&quot;: &quot;true&quot;,
	&quot;lastname&quot;: &quot;Payconiq&quot;
}

and correct way is this

{
	&quot;firstname&quot;: &quot;Tom&quot;,
	&quot;additionalneeds&quot;: &quot;Breakfast&quot;,
	&quot;bookingdates&quot;: {
		&quot;checkin&quot;: &quot;2023-06-06&quot;,
		&quot;checkout&quot;: &quot;2023-07-07&quot;
	},
	&quot;totalprice&quot;: &quot;677&quot;,
	&quot;depositpaid&quot;: &quot;true&quot;,
	&quot;lastname&quot;: &quot;Payconiq&quot;
}

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(&quot;firstname&quot;, &quot;Tom&quot;);
		request.put(&quot;lastname&quot;, &quot;Payconiq&quot;);
		request.put(&quot;totalprice&quot;, &quot;677&quot;);
		request.put(&quot;depositpaid&quot;, &quot;true&quot;);
		JSONObject bookingDates = new JSONObject();
		bookingDates.put(&quot;checkin&quot;, &quot;2023-06-03&quot;);
		bookingDates.put(&quot;checkout&quot;, &quot;2023-07-05&quot;);
		request.put(&quot;bookingdates&quot;, bookingDates);
		request.put(&quot;additionalneeds&quot;, &quot;Breakfast&quot;);

		System.out.println(request.toJSONString());
		//baseURI = &quot;https://restful-booker.herokuapp.com/booking&quot;;

		given().contentType(ContentType.JSON) .body(request.toJSONString()).when()
			.post(&quot;https://restful-booker.herokuapp.com/booking&quot;).then().statusCode(200).log()
			.all();
	}
}

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:

确定