简体   繁体   中英

Rest Assured: Fail to deserialize json response to its POJO

I have the following returned JSON from Azure login REST call:

{
    "token_type": "Bearer",
    "expires_in": "3600",
    "ext_expires_in": "3600",
    "expires_on": "1560857196",
    "not_before": "1560853296",
    "resource": "https://management.azure.com",
    "access_token": "d9f9s..." //I cut the value significantly here...
}

The POJO I built for it is as follows:

public class AzureLoginResponse {

    private String tokenType;
    private String expiresIn;
    private String extExpiresIn;
    private String expiresOn;
    private String notBefore;
    private String resource;
    private String accessToken; 

    //setters\getters

}

When I am doing this:

   @Given("^Azure Login Request Executed$")
    public void azureLoginExecuted() {

        RestAssured.baseURI = BASE_URI;

        Response response =
        given()  //Add x-www-form-urlencoded body params:
            .formParam(GRANT_TYPE_KEY, GRANT_TYPE_VALUE)
            .formParam(AUTO_TEAM_CLIENT_ID_KEY, AUTO_TEAM_CLIENT_ID_VALUE)
            .formParam(AUTO_TEAM_CLIENT_SECRET_KEY, AUTO_TEAM_CLIENT_SECRET_VALUE)
            .formParam(RESOURCE_KEY, RESOURCE_VALUE)
        .when()
            .post(AUTO_TEAM_TENANT_ID + RESOURCE); //Send the request along with the resource

        AzureLoginResponse azureLoginResponse = response.as(AzureLoginResponse.class);

    }

azureLoginResponse gets the following data in it:

azureLoginResponse = {AzureLoginResponse@3532} 
 tokenType = null
 expiresIn = null
 extExpiresIn = null
 expiresOn = null
 notBefore = null
 resource = "https://management.azure.com"
 accessToken = null

so, only 'resource' property is getting populated, whereas tests like

assertThat(expires_in_val, greaterThan(min_expected_expires_in_val));

or

response.then().body("resource", equalTo(expected_resource));

pass with no troubles.

Please advise. Thanks!

Your POJO keys doesn't match with the keys coming in response, hence the null value. As you can see the keys coming in response is snake_case and the one used in your POJO is camelCase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM