Wednesday, 14 February 2024

API 2

 

https://www.blogger.com/blog/post/edit/695939364463034111/8924884184314606966


Question: How do you handle scenarios where the API returns unexpected content types?
Answer: RestAssured allows you to validate unexpected content types using the contentType() method.

import static io.restassured.RestAssured.*;

public class UnexpectedContentTypeHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/resource")
        .then()
            .contentType("text/plain"); // Validates if the response content type is "text/plain"
    }
}

Question: How do you handle scenarios where the API returns unexpected HTTP status codes?
Answer: RestAssured allows you to validate unexpected HTTP status codes using the not() method. 

import static io.restassured.RestAssured.*;

public class UnexpectedStatusCodeHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/resource")
        .then()
            .statusCode(not(404)); // Validates if the response status code is not 404
    }
}

Question: How do you handle exceptions such as connection timeouts in RestAssured tests?
Answer: You can handle connection timeouts by setting a timeout duration using the timeout() method. Additionally, you can catch the java.net.SocketTimeoutException exception.

import static io.restassured.RestAssured.*;

public class ConnectionTimeoutHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        try {
            given()
                .get("/resource")
                .timeout(5000) // Set a timeout of 5 seconds
            .then()
                .statusCode(200);
        } catch (java.net.SocketTimeoutException e) {
            // Handle timeout exception
            System.out.println("Connection timed out.");
        }
    }
}

Question: How do you handle exceptions such as invalid SSL certificates in RestAssured tests?
Answer: You can handle invalid SSL certificates by disabling SSL certificate validation using the relaxedHTTPSValidation() method.


 
import static io.restassured.RestAssured.*;

public class InvalidSSLCertificateHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .relaxedHTTPSValidation()
            .get("/resource")
        .then()
            .statusCode(200);
    }
}

Question: How do you handle exceptions such as resource not found errors in RestAssured tests?
Answer: You can handle resource not found errors by expecting specific error responses using the expect() method.

import static io.restassured.RestAssured.*;

public class ResourceNotFoundErrorHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/nonexistent-resource")
        .then()
            .statusCode(404)
            .statusLine("HTTP/1.1 404 Not Found");
    }
}

Question: How do you handle exceptions such as JSON parsing errors in RestAssured tests?
Answer: You can handle JSON parsing errors by expecting specific error responses or by catching the io.restassured.path.json.exception.JsonPathException exception.

import static io.restassured.RestAssured.*;

public class JsonParsingErrorHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        try {
            given()
                .get("/malformed-json-resource")
            .then()
                .statusCode(200)
                .extract().jsonPath();
        } catch (io.restassured.path.json.exception.JsonPathException e) {
            // Handle JSON parsing error
            System.out.println("JSON parsing error occurred.");
        }
    }
}
Question: How do you handle exceptions such as server errors (5xx) in RestAssured tests?
Answer: You can handle server errors by expecting specific error responses using the expect() method. 
import static io.restassured.RestAssured.*;

public class ServerErrorHandlingExample {
    public static void main(String[] args) {
        baseURI = "https://api.example.com";

        given()
            .get("/error-endpoint")
        .then()
            .statusCode(500)
            .statusLine("HTTP/1.1 500 Internal Server Error");
    }
}

No comments:

Post a Comment

Featured

Software Testing

Question: What is software testing, and why is it important? Answer: Software testing is the process of evaluating a software application to...

popular