---Advertisement---

API Interview Questions and Answers (Level-2)

By Manisha

Published On:

---Advertisement---

6. How do you assert uniqueness of an ID in API testing?

In API testing, verifying the uniqueness of an ID is crucial to ensure that duplicate entries are not created for resources like users, products, or orders.

Approach:

  • First, retrieve the existing list of IDs by sending a GET request.
  • Then, check if the new ID already exists in the list.
  • Assert that the new ID does not exist, meaning it is unique.

Example using Rest-Assured (Java):

java

List<Integer> ids = given()

    .when()

    .get(“https://api.example.com/users”)

    .jsonPath()

    .getList(“id”);

int newId = 101;

assertFalse(ids.contains(newId)); // Assert that newId is not already present

Explanation:

  • We fetch all user IDs from the /users API.
  • We use an assertion (assertFalse) to ensure the list does not contain the newId.
  • If the ID already exists, the test will fail — ensuring uniqueness validation.

✅ This technique helps prevent duplicates and maintain data integrity in the database through API validation.


7. How do you verify the account numbers array for a bank user?

When a user has multiple accounts, you need to validate:

  • Whether all expected accounts are correctly retrieved.
  • Whether the response array contains all expected account numbers.

Approach:

  • Send a GET request to the user accounts endpoint.
  • Assert that the response contains all the expected account numbers.

Example using Rest-Assured:

java

given()

    .when()

    .get(“https://api.example.com/users/1/accounts”)

    .then()

    .statusCode(200)

    .body(“accounts”, hasItems(“123456”, “789012”, “345678”));

Explanation:

  • hasItems() checks if the specified account numbers are present in the response array.
  • This ensures that no account is missing.
  • You can also validate the total count of accounts if needed:

java

.body(“accounts.size()”, equalTo(3));

✅ Verifying array elements ensures complete and correct data is returned by the API for the user.


8. Represent the Users API response in the form of JSON.

When working with an API, understanding the expected JSON structure of the response is critical for creating validations and parsers.

Example JSON Structure of Users API:

json

{

  “id”: 1,

  “name”: “John Doe”,

  “email”: “john.doe@example.com”,

  “accounts”: [

    {

      “accountNumber”: “123456”,

      “balance”: 1000.0

    },

    {

      “accountNumber”: “789012”,

      “balance”: 2500.5

    }

  ]

}

Explanation of the JSON structure:

  • id: Unique identifier for the user (integer).
  • name: Name of the user (string).
  • email: Email address of the user (string).
  • accounts: An array containing multiple account objects:
    • Each account has an accountNumber (string) and a balance (float).

✅ Knowing the structure helps in writing precise assertions and handling nested data while parsing the API response.


9. What is a Mocking Response and How is it achieved?

Mocking refers to creating a simulated API server that returns fake (but realistic) responses for testing purposes — without depending on the live backend.

Why Mock Responses?

  • Backend services are still under development.
  • To test edge cases like server errors (500), timeouts, and unexpected formats.
  • To isolate frontend/client testing.

How to achieve Mocking?

  • Tools like WireMock, Postman Mock Server, Mockoon, or Swagger Mock Server can be used.

Example using WireMock (Java):

java

WireMockServer wireMockServer = new WireMockServer(); // Create server

wireMockServer.start(); // Start server

wireMockServer.stubFor(get(urlEqualTo(“/users/1”))

    .willReturn(aResponse()

        .withHeader(“Content-Type”, “application/json”)

        .withBody(“{ \”id\”: 1, \”name\”: \”John Doe\” }”)));

given()

    .when()

    .get(“http://localhost:8080/users/1”)

    .then()

    .statusCode(200)

    .body(“name”, equalTo(“John Doe”));

wireMockServer.stop(); // Stop server

Explanation:

  • Stub is created for the /users/1 endpoint to return a fake response.
  • API client sends a request to the local mock server instead of a real server.
  • The mock server returns the predefined response.

✅ Mocking is extremely useful for reliable, faster, and independent API automation testing.


10. What is API Automation Testing? How is it different from UI Automation Testing?

API Automation Testing:

  • Definition: Testing APIs by sending requests and validating the responses automatically using scripts/tools.
  • Focus: Verifying business logic, data correctness, status codes, performance, and error responses at the API layer.
  • Tools: Postman (with Newman), Rest-Assured, Karate, SoapUI, JMeter, etc.
  • Speed: Faster than UI testing because it does not load pages or graphical elements.
  • Stability: More stable; not affected by UI changes (buttons, CSS changes).

Example API Test:

java

given()

    .when()

    .get(“https://api.example.com/users/1”)

    .then()

    .statusCode(200)

    .body(“name”, equalTo(“John Doe”));


UI Automation Testing:

  • Definition: Testing the user interface (UI) of applications to ensure it behaves correctly when users interact with it.
  • Focus: Verifying page layouts, button clicks, forms, links, navigation, and visual correctness.
  • Tools: Selenium WebDriver, Cypress, Playwright, TestCafe, etc.
  • Speed: Slower compared to API testing due to loading and rendering elements.
  • Fragility: UI tests are more prone to breakage due to minor frontend changes.

Example UI Test (Selenium WebDriver Java):

java

WebDriver driver = new ChromeDriver();

driver.get(“https://example.com/login”);

driver.findElement(By.id(“username”)).sendKeys(“user”);

driver.findElement(By.id(“password”)).sendKeys(“pass”);

driver.findElement(By.id(“loginButton”)).click();

assertEquals(driver.getTitle(), “Dashboard”);

driver.quit();


In short:

  • API Testing = Backend logic verification
  • UI Testing = Frontend user interaction verification
  • Ideal Approach: Perform API automation for core business functionality first (fast and stable) and UI automation afterward for end-to-end scenarios.

👉The Next 5 Questions 3: API Method

---Advertisement---

Leave a Comment