java.lang.AssertionError: 1 expectation failed. Expected status code <201> but was <400>

Viewed 21

Hi I am trying to do a post request using rest assured. But I am gettin server error 400(BAD Request) java.lang.AssertionError: 1 expectation failed. Expected status code <201> but was <400>..I searched for method called toJSONString for serialization but it was absent in autosugestion in eclipse please help me I am new to this

Code:

@Test
public void UserRegistrationSuccessful2() 
{ 

    Map<String, Object> map = new HashMap<String, Object>();

    map.put("name", "Raghav");
    map.put("job", "Teacher");

    System.out.println(map);
//
    JSONObject request = new JSONObject();
    request.put("name", "Raghav");
    request.put("job", "Teacher");
    given().
    header("Content-Type","application/json")
    .contentType(ContentType.JSON)
    .accept(ContentType.JSON)
    .body(request.toString())
    .when()
    .post("https://reqres.in/api/register")
    .then()
    .statusCode(201)
    
    ;

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Rest</groupId>
  <artifactId>Rest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
          <dependency>
                  <groupId>io.rest-assured</groupId>
                  <artifactId>rest-assured</artifactId>
                  <version>5.1.1</version>  
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20220320</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
                <dependency>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                    <version>2.9.1</version>
                </dependency>

  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>16</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
0 Answers
Related