×
☰ See All Chapters

REST API Testing with Karate Example

Create Java Project using Maven

In the command prompt execute the following maven command to generate Maven supported Java project name as “RESTWebServiceTestingUsingKarate”.

mvn archetype:generate -DgroupId=com.tools4testing -DartifactId=RESTWebServiceTestingUsingKarate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

rest-api-testing-with-karate-example-0
 

This command creates a new maven Java project with the name “RESTWebServiceTestingUsingKarate”, with complete directory structure.

rest-api-testing-with-karate-example-1
 

Convert to eclipse project

To import Maven project into Eclipse IDE, in terminal, navigate inside “RESTWebServiceTestingUsingKarate” project (folder should has pom.xml file), and issue mvn eclipse:eclipse command.

rest-api-testing-with-karate-example-2
 
rest-api-testing-with-karate-example-3
 

Import converted project into Eclipse IDE

In Eclipse IDE, Choose File –> Import –> General -> Existing Projects into Workspace –>Choose your project folder location. Done

rest-api-testing-with-karate-example-4
 

Add dependencies in pom.xml

Add the below spring boot, karate and other dependencies in pom.xml file.

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.tools4testing</groupId>

        <artifactId>RESTWebServiceTestingUsingKarate</artifactId>

        <version>1.0-SNAPSHOT</version>

        <name>RESTWebServiceTestingUsingKarate</name>

        <properties>

                <java.version>11</java.version>

                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

                <spring-boot.version>2.3.1.RELEASE</spring-boot.version>

                <maven.compiler.source>11</maven.compiler.source>

                <maven.compiler.target>11</maven.compiler.target>

        </properties>

        <dependencies>

                <dependency>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-starter</artifactId>

                </dependency>

 

                <dependency>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-starter-web</artifactId>

                </dependency>

                <dependency>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-starter-test</artifactId>

                        <version>2.3.1.RELEASE</version>

                        <scope>test</scope>

                </dependency>

 

                <dependency>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-starter-test</artifactId>

                        <scope>test</scope>

                </dependency>

                <dependency>

                        <groupId>org.apache.commons</groupId>

                        <artifactId>commons-text</artifactId>

                        <version>1.9</version>

                </dependency>

                <dependency>

                        <groupId>com.intuit.karate</groupId>

                        <artifactId>karate-apache</artifactId>

                        <version>0.9.5</version>

                        <scope>test</scope>

                </dependency>

                <dependency>

                        <groupId>com.intuit.karate</groupId>

                        <artifactId>karate-junit4</artifactId>

                        <version>0.9.5</version>

                        <scope>test</scope>

                </dependency>

 

        </dependencies>

        <dependencyManagement>

                <dependencies>

                        <dependency>

                                <groupId>org.springframework.boot</groupId>

                                <artifactId>spring-boot-dependencies</artifactId>

                                <version>${spring-boot.version}</version>

                                <type>pom</type>

                                <scope>import</scope>

                        </dependency>

                </dependencies>

        </dependencyManagement>

        <build>

                <testResources>

                        <testResource>

                                <directory>src/test/java</directory>

                                <excludes>

                                        <exclude>**/*.java</exclude>

                                </excludes>

                        </testResource>

                </testResources>

                <plugins>

                        <plugin>

                                <groupId>org.springframework.boot</groupId>

                                <artifactId>spring-boot-maven-plugin</artifactId>

                                <version>${spring-boot.version}</version>

                                <executions>

                                        <execution>

                                                <id>build-info</id>

                                                <goals>

                                                        <goal>build-info</goal>

                                                        <goal>repackage</goal>

                                                </goals>

                                        </execution>

                                </executions>

                        </plugin>

                </plugins>

        </build>

</project>

 

Issue the “mvn eclipse:eclipse“, in command prompt, Maven will download all dependencies automatically and put into Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.

Enable maven nature for project

Right click on the project -> Configure -> Convert to Maven Project. This will bring maven nature to the project which helps in running test cases.

You can check the Letter M is now displayed on Project folder.

rest-api-testing-with-karate-example-5
 

Create sample rest controller

DemoRestController.java

package com.tools4testing.restcontroller;

 

import org.springframework.stereotype.Service;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

 

@RestController

@Service

public class DemoRestController {

        @GetMapping("/hello")

        public String sayHello() {

                return "HelloWorld";

        }

}

 

Create spring boot runner class

KarateSprintBootApplication.java

package com.tools4testing.main;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

 

@SpringBootApplication

@ComponentScan(basePackages = {"com.tools4testing"})

public class KarateSprintBootApplication {

    public static void main(String[] args) {

        SpringApplication.run(KarateSprintBootApplication.class, args);

    }

}

Create test script feature file and test runner file

testRestService.feature

Feature: fetching key value

 

    Scenario: testing the get call for key value

        Given url 'https://localhost:8080/hello'

        When method GET

        Then status 200

        And match $ == "HelloWorld"

 

testRestService.feature

package karate.testscripts;

 

import com.intuit.karate.junit4.Karate;

import org.junit.runner.RunWith;

 

@RunWith(Karate.class)

public class TestRunner {

 

}

Karate test scripts are written in files with “.feature” extension. You can keep the files anywhere inside the project as the project requirement for better organization of files.  However, the Maven guidelines instruct that the storage of non-Java files to be done separately. They are done in a src/test/resources structure. And the Java files are kept under src/test/java.

But as per the creators of the Karate Framework, they strongly believe that we keep both Java and non-Java files side by side. As per them, it is much easier to look out for the *.java and *.feature files when they are kept together, rather than following the standard Maven structure.

Final Directory Structure of Project in eclipse

rest-api-testing-with-karate-example-6
 

Run the test scripts

Before running test scripts make sure REST service is available, so run the spring boot application which brings the REST service up. Right click on KarateSprintBootApplication.java and select Run As -> Java Application.

rest-api-testing-with-karate-example-7
 

Now you Right click on TestRunner.java and select Run As -> Junit Test. You should see the scenarios executed and their pass status.

rest-api-testing-with-karate-example-8
 

Karate generates HTML report which you can check it browser, copy the HTML report file location provided in the console and open in browser.

rest-api-testing-with-karate-example-9
 

Run the test scripts from maven build

In terminal, navigate inside “RESTWebServiceTestingUsingKarate” project (folder should has pom.xml file), and issue mvn clean install command.

rest-api-testing-with-karate-example-10
 
rest-api-testing-with-karate-example-11
 

All Chapters
Author