简体   繁体   中英

"POST" method is throwing 500 code using rest-assured

I created a post request using Student POJO in REST Assured libraries. Also added gson object serializer in pom.xml. However my code is not posting anything and I received 500 response code.

Any help regarding this would be highly appreciable.

When I pass the JSON as a string my code is working. It's only not working when passing the student object.

@Test
public void createNewStudent() {
    Student student = new Student();
    Faker fake = new Faker();
    List<String> courses = new ArrayList<String>();
    courses.add("Accounting");
    courses.add("Statistics");              
    student.setFirstname(fake.name().firstName());
    student.setLastname(fake.name().lastName());
    student.setEmail(fake.internet().emailAddress());
    student.setProgramme("Financial Analysis");
    student.setCourses(courses);
    
    
    given()
    .when()
    .contentType(ContentType.JSON)
    .body(student)
    .post()
    .then()
    .statusCode(201)
    ;
}

Studet POJO

public class Student {  
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getProgramme() {
        return programme;
    }
    public void setProgramme(String programme) {
        this.programme = programme;
    }
    public List<String> getCourses() {
        return courses;
    }
    public void setCourses(List<String> courses) {
        this.courses = courses;
    }
    private String firstname;
    private String lastname;
    private String email;
    private String programme;
    private List<String> courses;
    
}   

You can try jackson. Don't know why gson is not working.

<dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.10.3</version>
</dependency>

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