☰ See All Chapters |
Testing REST Service Response Example using Karate
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>KarateTestingResponse</artifactId> <version>1.0-SNAPSHOT</version> <name>KarateTestingResponse</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> |
DemoRestController.java
package com.tools4testing.restcontroller;
import org.apache.commons.text.CaseUtils; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController;
import com.tools4testing.dto.Employee; import com.tools4testing.dto.WordResponse;
@RestController @Service public class DemoRestController {
@GetMapping("/hello") public String sayHello() { return "HelloWorld"; }
@GetMapping("/data/{word}") public WordResponse getWordDetails(@PathVariable(value = "word", required = true) String word) { WordResponse response = new WordResponse(); response.setCapitalizeFirstLetter(CaseUtils.toCamelCase(word, true)); response.setNumberOfCharacters(word.toCharArray().length); response.setAlphaNumeric(word.chars().allMatch(Character::isLetterOrDigit)); return response; }
@PostMapping("/saveEmp") public Employee saveEmployee(@RequestBody Employee employee) throws AgeNotSupportedException { //{"fullName":"Manu Manjunatha","gender":"Male","age":20} if (employee.getAge() > 18) { employee.setId(100); return employee; } else { throw new AgeNotSupportedException("Age is less than 18"); } } }
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Age is less than 18") class AgeNotSupportedException extends RuntimeException { public AgeNotSupportedException (String message) { super(message); } }
|
Employee.java
package com.tools4testing.dto;
public class Employee { private String fullName; private int id; private String gender; private int age;
public String getFullName() { return fullName; }
public void setFullName(String fullName) { this.fullName = fullName; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
|
WordResponse.java
package com.tools4testing.dto;
public class WordResponse { private String capitalizeFirstLetter; private int numberOfCharacters; private boolean isAlphaNumeric;
public String getCapitalizeFirstLetter() { return capitalizeFirstLetter; }
public void setCapitalizeFirstLetter(String capitalizeFirstLetter) { this.capitalizeFirstLetter = capitalizeFirstLetter; }
public int getNumberOfCharacters() { return numberOfCharacters; }
public void setNumberOfCharacters(int numberOfCharacters) { this.numberOfCharacters = numberOfCharacters; }
public boolean isAlphaNumeric() { return isAlphaNumeric; }
public void setAlphaNumeric(boolean isAlphaNumeric) { this.isAlphaNumeric = isAlphaNumeric; }
} |
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); }
} |
TestRunner.java
package karate.testscripts;
import com.intuit.karate.junit4.Karate; import org.junit.runner.RunWith;
@RunWith(Karate.class) public class TestRunner {
} |
testResponse.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"
Scenario: testing word response service Given url 'https://localhost:8080/data/helloworld' #And request "HelloWorld" When method GET Then status 200 And match $.capitalizeFirstLetter == "Helloworld" And match $.numberOfCharacters == 10 And match $.alphaNumeric == true
Scenario: testing employee service Given url 'https://localhost:8080/saveEmp' And request '{"fullName":"Manu Manjunatha","gender":"Male","age":20}' And header Content-Type = 'application/json' And header User-Agent = 'karate' And header Accept = '*/*' When method POST Then status 200 And match response == { "fullName": "#string", "id": "#notnull", "gender": "#string", "age": "#number" }
Scenario: testing employee service age less than 18 Given url 'https://localhost:8080/saveEmp' And request '{"fullName":"Manu Manjunatha","gender":"Male","age":10}' And header Content-Type = 'application/json' And header User-Agent = 'karate' And header Accept = '*/*' When method POST Then status 400 |
Project Directory Structure
Output
All Chapters