How to Test Lombok API by Playwright

 

To test a Lombok-based API using Playwright with Java, you need to understand that Lombok is a Java annotation library used to reduce boilerplate code in Java classes. When we say "Lombok API", we are usually referring to a REST API that uses Lombok in its backend for DTOs or models.

From a test automation perspective using Playwright Java, it doesn’t matter if Lombok is used on the server side. Your focus will be on sending HTTP requests (GET, POST, PUT, DELETE) and validating responses (status codes, response body, etc.).



Steps to Test a Lombok-based API in Playwright Java:

  • Set up Playwright Java Project
  • Use Java’s HttpClient or OkHttp for API calls
  • Send a request to the API
  • Validate the response (status, body, etc.)




Example

Assume we have a Lombok-powered API:


POST http://localhost:8080/api/users
Request Body:
{
  "name": "John Doe",
  "email": "john@example.com"
}




Response:


{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com"
}






Code to Test this API using Playwright Java + HttpClient


import com.microsoft.playwright.*;
import java.net.URI;
import java.net.http.*;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpRequest.BodyPublishers;

public class LombokApiTest {
    public static void main(String[] args) throws Exception {
        // Start Playwright (in case you want to verify UI after API test)
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();

            // Send API request using Java HttpClient
            HttpClient client = HttpClient.newHttpClient();

            // Sample JSON payload
            String jsonBody = """
                    {
                      "name": "John Doe",
                      "email": "john@example.com"
                    }
                    """;

            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:8080/api/users"))
                    .header("Content-Type", "application/json")
                    .POST(BodyPublishers.ofString(jsonBody))
                    .build();

            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

            // Print and assert
            System.out.println("Status Code: " + response.statusCode());
            System.out.println("Response Body: " + response.body());

            if (response.statusCode() == 200 || response.statusCode() == 201) {
                System.out.println("API Test Passed");
            } else {
                System.out.println("API Test Failed");
            }

            // Optionally, use Playwright to navigate to a UI and verify the new user
            // page.navigate("http://localhost:8080/users");
            // page.locator("text=John Doe").waitFor(); // if user is listed
        }
    }
}



Tools Used

  • Java HttpClient: To test the REST API.

  • Playwright Java: To optionally validate frontend after API call.

  • Lombok: Present in the backend API but irrelevant to test logic.





Maven Dependency for Playwright


<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.43.0</version> <!-- or latest -->
</dependency>



No comments:

Post a Comment