java.lang.AssertionError: 1个期望未达成。期望的状态码为<200>,但实际为<500>。

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

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

问题

我正在尝试进行一个PATCH调用,希望能更新一条记录,但是我收到了500错误。我已经检查过端点是否正确。测试代码如下:

import org.json.simple.JSONObject;
import org.testng.annotations.Test;
import io.restassured.http.ContentType;
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");
        
        System.out.println(payload);
        System.out.println(payload.toJSONString());
        
        given().
        header("Authorization", "Basic YWRtaW46cGFzc3dvcmQxMjM=").
        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();
    }
}

我期望代码能够正确运行并返回状态码200,但实际上返回了状态码500,这意味着它无法连接到服务器本身。该请求在Postman中运行良好。有人可以提供帮助吗?

英文:

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(&quot;firstname&quot;,&quot;Tiger&quot;);
		payload.put(&quot;lastname&quot;,&quot;Test&quot;);
		//String AuthToken =&quot;Basic YWRtaW46cGFzc3dvcmQxMjM=&quot;;
		
		System.out.println(payload);
		System.out.println(payload.toJSONString());
		
		given().
	    header(&quot;Authorization&quot;, &quot;Basic YWRtaW46cGFzc3dvcmQxMjM=&quot;).
		//header(&quot;Authorization&quot;,&quot;AuthToken&quot;).
		contentType(ContentType.JSON).
		accept(ContentType.JSON).
		header(&quot;cookie&quot;,&quot;token=abc123&quot;).
		body(payload.toJSONString()).
		when().
		patch(&quot;https://restful-booker.herokuapp.com/booking/2&quot;).
		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?

答案1

得分: 0

问题是这里的ContentType.JSON不等于application/json。对我来说,这似乎是一个错误,但我不知道背后的原因。

修复方法将是

contentType("application/json").
accept("application/json").
英文:

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(&quot;application/json&quot;).
accept(&quot;application/json&quot;).

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

发表评论

匿名网友

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

确定