线程“主”java.lang.RuntimeException中的异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:

问题描述 投票:0回答:1

我正在尝试为电子商务项目创建 E2E Rest-Assured API 自动化流程。

我正在使用 Pojo 类和序列化/反序列化来帮助解决这个问题。

在这里,我尝试使用参数“/api/ecom/auth/login”登录名为 https://rahulshettyacademy.com 的 BaseUri。我已经在 Postman 中使用我的登录名和密码手动测试了这一点,并且工作正常。但我在自动化方面遇到了问题。

这是我的主要 API 类的代码:

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import pojo.EcommerceLoginRequest;
import pojo.EcommerceLoginResponse;


import static io.restassured.RestAssured.given;


public class ECommerceAPITest {

    public static void main(String[] args) {

        
     RequestSpecification req = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com").setContentType(ContentType.JSON).build();
     
     EcommerceLoginRequest loginRequest = new EcommerceLoginRequest();
     loginRequest.setUserEmail("[email protected]");
     loginRequest.setUserPassword("786Immy110.1");

     
    RequestSpecification reqLogin = given().log().all().spec(req).body(loginRequest); 
    EcommerceLoginResponse loginResponse = reqLogin.when().post("/api/ecom/auth/login").then().log().all().extract().response().as(EcommerceLoginResponse.class); 

    
    
    System.out.println(loginResponse.getToken());
    System.out.println(loginResponse.getuserid());
    
    
    
    }

}

以下是 pojo 类中名为“EcommerceLoginResponse”的代码:

package pojo;


public class EcommerceLoginResponse {
    
    String token;
    String userid;
    String message;
    
    public String getToken() {
        return token;
    }
    public void setToken(String token) {
        this.token = token;
    }
    public String getuserid() {
        return userid;
    }
    public void setuserid(String userid) {
        this.userid = userid;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    

}

运行主测试后,我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "userId" (class pojo.EcommerceLoginResponse), not marked as ignorable (3 known properties: "token", "userid", "message"])
 at [Source: (String)"{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NGVhMmQ1MDc1M2VmYTQ2NTdmMDBiNDMiLCJ1c2VyRW1haWwiOiJpbXRpeWF6X0Bob3RtYWlsLmNvLnVrIiwidXNlck1vYmlsZSI6MTIzNDU2Nzg5MSwidXNlclJvbGUiOiJjdXN0b21lciIsImlhdCI6MTY5MzA3NjIwNywiZXhwIjoxNzI0NjMzODA3fQ.SZYf86-UPkQwy9UmZEsbt16cNDmYfnLcpRLgGa_aa1A","userId":"64ea2d50753efa4657f00b43","message":"Login Successfully"}"; line: 1, column: 306] (through reference chain: pojo.EcommerceLoginResponse["userId"])

关于我哪里出错了有什么想法吗?

java serialization automation deserialization rest-assured
1个回答
0
投票

问题是响应键是

userId
而不是
userid

解决方案1:更改属性名称和getter/setter。

String userId;

public String getUserId() {
    return userId;
}
public void setUserId(String userid) {
    this.userId = userid;
}

解决方案2:使用Jackson注解并且不关心属性名称和getter/setter

@JsonProperty("userId")
String userid;
© www.soinside.com 2019 - 2024. All rights reserved.