我是REST Assured API测试的新手,我已经在库中添加了jackson-databind,jackson-annotations和jackson-core所有2.9.6版本。
下面是我的代码:
public class SerializeAndDeserialize {
@Test
public void RegistrationSuccessful()
{
RestAssured.baseURI ="http://restapi.demoqa.com/customer";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("FirstName", "Virkjdender"); // Cast
requestParams.put("LastName", "Scklxjingh");
requestParams.put("UserName", "63userf2d3cn,mxd2011");
requestParams.put("Password", "passwors000mxzmd1");
requestParams.put("Email", "ed26dfdasdf39@gmail.com");
request.body(requestParams.toJSONString());
Response response = request.post("/register");
ResponseBody body = response.getBody();
System.out.println(response.body().asString());
System.out.println(response.contentType());
System.out.println(response.getStatusCode());
if(response.statusCode() == 200)
{
// Deserialize the Response body into RegistrationFailureResponse
RegistrationFailureResponse responseBody = body.as(RegistrationFailureResponse.class);
Assert.assertEquals("User already exists", responseBody.FaultId);
Assert.assertEquals("FAULT_USER_ALREADY_EXISTS", responseBody.fault);
}
else if (response.statusCode() == 201)
{
// Deserialize the Response body into RegistrationSuccessResponse
RegistrationSuccessResponse responseBody = body.as(RegistrationSuccessResponse.class);
Assert.assertEquals("OPERATION_SUCCESS", responseBody.SuccessCode);
Assert.assertEquals("Operation completed successfully", responseBody.Message);
}
}
}
相关类如下:
public class RegistrationFailureResponse {
@JsonIgnoreProperties(ignoreUnknown = true)
String FaultId;
String fault;
}
另一个:
public class RegistrationSuccessResponse {
@JsonIgnoreProperties(ignoreUnknown = true)
public String SuccessCode;
public String Message;
}
当我尝试通过Testng.xml文件运行以上代码时,出现以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ FaultId”(类RegistrationFailureResponse),未标记为可忽略(0个已知属性:]) 在[来源:(字符串)” { “ FaultId”:“用户已经存在”, “故障”:“ FAULT_USER_ALREADY_EXISTS” }“;第2行,第17列](通过参考链:RegistrationFailureResponse [” FaultId“]) 在com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60) 在com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822) 在com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152) 在com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589) 在com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567) 在com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294) 在com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) 在com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) 在com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042) 在com.fasterxml.jackson.databind.ObjectMapper $ readValue $ 0.call(未知源) 在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) ......
请帮助,我用谷歌搜索了解决方案,但都与我在这里面临的问题无关
答案 0 :(得分:0)
实际上,您不应该忽略未知属性,因为没有属性。响应正是您所期望的:
{
"FaultId": "User already exists",
"fault": "FAULT_USER_ALREADY_EXISTS"
}
但是您的响应类配置错误。尝试使用@JsonProperty注释字段。
public class RegistrationFailureResponse {
@JsonProperty private String FaultId;
@JsonProperty private String fault;
}