I am trying to create an E2E Rest-Assured API automation flow for an E-commerce project.
I am using Pojo classes and serialisation/deserialisation in order to help with this.
Here I am trying to login to BaseUri called https://rahulshettyacademy.com with parameters '/api/ecom/auth/login'. I have tested this manually with my login and password in Postman & it is working fine. But I am having issues in automating it.
This is my code for the main API class:
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("imtiyaz_@hotmail.co.uk");
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());
}
}
The below is the code from the pojo class which is called '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;
}
}
Upon running the main test, I am getting the following error:
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"])
Any ideas on where I am going wrong?
The issue is response key is userId
not userid
.
Solution 1: change the property name and getter/setter.
String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userid) {
this.userId = userid;
}
Solution 2: use Jackson annotation and don't care the properties name and getter/setter
@JsonProperty("userId")
String userid;