DEV Community

keploy
keploy

Posted on • Originally published at keploy.io

Looking to migrate from REST Assured to Keploy?

Image description

If you're tired of writing endless lines of repetitive code in RestAssured just to test your APIs, you're not alone. API testing shouldn’t feel like pulling teeth, but let’s face it—REST Assured can make the process boring and unnecessarily time-consuming. But what if you could leave that grind behind?

In this guide, we’ll show you how to make the switch to Keploy, a smarter, zero-code way to test your APIs. Let’s make your API testing faster, easier, and, dare we say it, fun! Ready for the upgrade?

REST Assured Overview

REST Assured is a popular Java library used for testing RESTful web services. It provides a domain-specific language (DSL) for writing tests, allowing developers to validate the responses from APIs effectively. With features like:

  • Easy integration with testing frameworks like JUnit and TestNG.

  • Support for various HTTP methods (GET, POST, PUT, DELETE).

However, as APIs grow more complex and testing demands increase, relying on REST Assured can become a real pain. Let’s face it:

  • Manual test writing becomes repetitive and time-consuming.

  • Test maintenance is a nightmare, especially when APIs evolve.

  • Coverage reporting is not inbuilt and requires setup with libraries like Jacoco.

  • Complex setup eats into development time, distracting engineers from core tasks.

This is where Keploy steps in. Keploy automates the testing process, reducing engineering effort by at least 20% and letting your team focus on what matters—delivering high-quality software.

Why Migrate to Keploy?

Keploy is an open-source tool designed to automate API testing by capturing API Interactions and replaying them later. Some of its key features include:

  • Autogenerate Data Mocking: Keploy can automatically generate mocks based on interactions with various dependencies such as microservices and databases, reducing the need for manual mock creation.

  • Low-cost execution: Keploy doesn’t require a dedicated, complex test environment setup. This leads to less overhead in managing parallel environments and reduces costs associated with infrastructure.

  • Zero-code Testing: Unlike RestAssured, where developers need to write every test manually, Keploy offers a zero-code approach by capturing API interactions and generating tests automatically.

  • Easy Integration: It integrates well with CI/CD pipelines and other testing tools such as JUnit, TestNG, GitHub Action, etc.

  • Comprehensive Test Coverage: Since Keploy captures real-world API interactions, including edge cases, it helps ensure a broader and more realistic test coverage compared to the manually written tests in RestAssured.

Steps to Migrate from REST Assured to Keploy

We will run a simple employee-manager application in Java with Postgres as a database for this guide.

Step 1: Assess Your Current Test Suite

Before migrating, conduct a comprehensive assessment of your existing RestAssured test suite:

  • Identify Existing Test Cases: Document all existing test cases and their functionality.

  • Note Dependencies: Identify any dependencies that might affect the migration process.

Let’s run our test cases and check if everything is working fine or not

mvn test
Enter fullscreen mode Exit fullscreen mode

We will observe that all our test cases have passed, and since we have jacoco installed, we can also find out the coverage.

We have gotten around 68% of coverage for our test suite.

Let’s move ahead to set up keploy and our migration process.

Step 2: Set Up Keploy in Your Environment

  1. Install Keploy: You can set up Keploy by following the installation instructions on the Keploy GitHub repository.

  2. You can verify the installation by running the command Keploy on the terminal, we should be able to see the below output:

Step 3: Migrate Test Cases

Let’s start the process of migrating our existing REST Assured test cases.

    @BeforeEach
    public void setUp() {
        RestAssured.baseURI = "http://localhost";
        RestAssured.port = 8080;

        // Clean up repository to ensure fresh data for each test
        employeeRepository.deleteAll();

        // Create and save test employee
        testEmployee = new Employee();
        testEmployee.setFirstName("John");
        testEmployee.setLastName("Doe");
        testEmployee.setEmail("john.doe@example.com");
        employeeRepository.save(testEmployee);  // save to generate ID
    }
Enter fullscreen mode Exit fullscreen mode

Since our application runs locally on port 8080, we have configured RestAssured.port to also run on 8080, allowing Keploy to capture the API Interaction and create a new test suite when the REST Assured TestSuite is executed.

  1. Let’s create a jar file for our application by running mvn clean install -Dmaven.test.skip=true.

  2. With the Jar file ready, let’s start keploy in record mode to capture the test cases. Now it's time to bring our database up and running using docker-compose up postgres: -

  3. On the new terminal, let’s run keploy record -c "java -jar target/springbootapp-0.0.1-SNAPSHOT.jar": -

  4. Now, we have everything ready and set up to migrate our test suites. It’s time to run our existing REST Assured test suite. This execution will allow Keploy to capture the API requests and responses.

    Each of the test cases generated by keploy is a REST Assured test case: -

    @Test
    public void testGetEmployeeById() {
        get("/api/employees/" + testEmployee.getId())
            .then()
            .statusCode(200)
            .body("id", equalTo((int) testEmployee.getId()))  // Ensure correct ID
            .body("firstName", equalTo("John"))
            .body("lastName", equalTo("Doe"))
            .body("email", equalTo("john.doe@example.com"));
    }

    @Test
    public void testUpdateEmployee() {
        String updatedEmployeeJson = "{\"firstName\":\"Jane\",\"lastName\":\"Doe\",\"email\":\"jane.doe@example.com\"}";

        given()
            .contentType(ContentType.JSON)
            .body(updatedEmployeeJson)
        .when()
            .put("/api/employees/" + testEmployee.getId())
        .then()
            .statusCode(200)
            .body("firstName", equalTo("Jane"))
            .body("lastName", equalTo("Doe"))
            .body("email", equalTo("jane.doe@example.com"));
    }

    @Test
    public void testDeleteEmployee() {
        delete("/api/employees/" + testEmployee.getId())
            .then()
            .statusCode(200);

        // Verify that employee no longer exists
        get("/api/employees/" + testEmployee.getId())
            .then()
            .statusCode(404);
    }
Enter fullscreen mode Exit fullscreen mode

Step 4: Next Steps

We have successfully migrated REST Assured test cases to the keploy test suite. Below is one of the such keploy test case: -

So, let’s run our keploy test suite by running - keploy test -c "java -jar target/springbootapp-0.0.1-SNAPSHOT.jar" --delay 10: -

Since we have noise, that’s why one test failed, and we got around ~70.5% of coverage with keploy.

Conclusion

Migrating from REST Assured to Keploy offered various advantages, such as zero-code testing, real-time feedback, and streamlined CI/CD integration. By following the steps outlined in this guide, you can ensure a smooth transition while maximising the benefits of Keploy for your API testing needs.

By adopting Keploy, your development team can focus more on delivering high-quality software with reduced engineering effort, ultimately leading to improved productivity and software quality.

References:

  1. CI/CD - https://keploy.io/docs/ci-cd/jenkins/

  2. Get the Cloud trial - https://keploy.io/docs/keploy-cloud/cloud-installation/

Top comments (0)