0

I am trying to convert nested JSON to Java objects and perform API testing using restAssured. I have created Java class for JSON objects and test class using TestNG and restAssured. I am getting compilation error (java: incompatible types: java.lang.String cannot be converted to Id) in test class while trying to call set method.

Error : setId (**.Id) in MyPojo cannot be applied to (java.lang.string)

Created 'MyPojo' and 'Id' Java object class for JSON object and TestNG test method : createTestRun

Nested Json:

{
"Id":{
    "Status":"PASSED",
    "Message":"Run from restAssured",
    "Number": "123"
     }
 }

Class:MyPojo

    public class MyPojo
    {
        private Id Id;

        public Id getId ()
        {
            return Id;
        }

        public void setId (Id Id)
        {
            this.Id = Id;
        }

        @Override
        public String toString()
        {
            return "ClassPojo [Id = "+Id+"]";
        }
    }

Class: Id

    public class Id
    {
        private String Status;

        private String Message;

        private String Number;

        public String getStatus ()
        {
            return Status;
        }

        public void setStatus (String Status)
        {
            this.Status = Status;
        }

        public String getMessage ()
        {
            return Message;
        }

        public void setMessage (String Message)
        {
            this.Message = Message;
        }

        public String getNumber ()
        {
            return Number;
        }

        public void setNumber (String Number)
        {
            this.Number = Number;
        }

        @Override
        public String toString()
        {
            return "ClassPojo [Status = "+Status+", Message = "+Message+", Number = "+Number+"]";
        }
    }

Test Class :

    @Test
 public void createTestRun() {

    Id id = new Id();
    MyPojo myPojo = new MyPojo();

    myPojo.setId("23685") // Getting Error in this line of code
    id.setSuccess("PASSED");
    id.setConclusion("Run from restAssured");
    id.setRunTime("123");

    Response response = given()
            .auth().preemptive()
            .basic(propertyFile.getUserName(),propertyFile.getPassword())
            .contentType(ContentType.JSON)
            .accept(ContentType.JSON)
            .when()
            .body(id)
            .post(ROOT_URI + TEST_RUN_ID + "/result");
            .then()
            .extract()
            .response();
          }

1 Answer 1

2

public void setId (Id Id) method signature requires the argument to be of type Id and not String. You are passing a string in the line myPojo.setId("23685"). Change it to myPojo.setId(id)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.