Skip to main content

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");
    }
}

Comments

popular

Privacy policy for BMI calculator

Privacy Policy for BMI Calculator  Effective Date: 5th July 2023 1.1 Personal Information: We do not collect any personally identifiable information (PII) such as your name, address, email, or phone number when you use the App. 1.2 Non-Personal Information: The App may collect certain non-personal information automatically, such as your device's unique identifier (UDID), device type, operating system, language preferences, and anonymous usage statistics. This information is collected to improve the functionality and user experience of the App and is not linked to any personally identifiable information. 2. Use of Information: 2.1 Personal Information: As stated earlier, we do not collect any personal information through the App. Therefore, we do not use or share any personal information. 2.2 Non-Personal Information: The non-personal information collected by the App may be used for the following purposes: - To improve the performance, functionality, and user experience of the App. ...

Privacy policy for Sri Bhagavat Gita

 Privacy Policy for Sri Bhagavad Gita This respects the privacy of its users and is committed to protecting their personal information. This privacy policy outlines the information collected by This and how it is used. Information We Collect : We are not collecting any personal information such as name and email address. This may collect non-personal information such as device type, operating system version, and app usage data to improve the app's functionality and user experience. Sharing of Information This does not sell or share personal information with third parties for marketing purposes. This may share personal information with service providers for the purpose of providing registration or support services to the user. Security of Information This takes reasonable measures to protect user data against unauthorized access, alteration, or destruction. However, This cannot guarantee the security of user data transmitted over the internet. Children's Privacy This does not kn...

Software Testing

Question: What is software testing, and why is it important? Answer: Software testing is the process of evaluating a software application to identify and rectify defects or issues. It ensures that the software meets the specified requirements, functions correctly, and is reliable.  It's important because it helps improve the quality of the software, reduces the risk of defects in production, and enhances user satisfaction. Question:   Software Development Life Cycle(SDLC) Answer: SDLC stands for Software Development Life Cycle. It is a structured and systematic approach to planning, creating, testing, deploying, and maintaining software applications or systems. SDLC provides a framework for software development teams to follow in order to ensure that software projects are completed efficiently, with high quality, and within budget. The SDLC process typically consists of several stages or phases, which can vary in number and specific activities depending on the chosen methodolo...