An Introduction to Testing Web Services with Karate and JUnit


Karate is a new test framework based on the well-known Cucumber library. It allows you to quickly write meaningful tests for web services using a domain-specific language (DSL).

Karate offers some interesting features such as: Native JSON and XML support, reusable script and feature definitions, an integrated JavaScript engine which lets you define functions in JavaScript, support for configuration changes and staging, multi-threaded parallel execution of tests, and the ability to execute Java classes.

Other features are documented on the Karate project's GitHub page.


You will need to install these two  tools to implement the following examples yourself:

Testing a web service

We are testing a web service that provides us with current news in the following JSON format:

  "message": "Urgent message: The sun is shining!",
  "id": 12345

This service is available at: http://localhost:3000/news (GET).

Test setup

First, we need to add the Karate library dependency to thepom.xml file in our Maven project:


Then we add a Java class so that our tests will be recognized by the JUnit Runner and other standard tools like the Maven Surefire plugin:

import org.junit.runner.RunWith;
public class NewsTest {}

We store the service address in a JavaScript configuration file called karate-config.js, so that we can easily update or exchange it for staging and execution on the integration server:

function() {
  return {
    baseUrl: 'http://localhost:3000'

Now we can code our test in a file named news.feature.

We check that the following expressions are true:

  • The HTTP status code in the answer is 200.
  • In the returned JSON structure the value of the message is not null.
  • And the ID is in a numerical format.

A number of predefined markers are available - for example:

  • #ignore – Ignore the field.
  • #null – The the value must be null.
  • #notnull –The the value must be not-null.
  • #array – The the value must be a JSON array.
  • #object – The the value must be a JSON object.
  • #boolean – The actual value must be a boolean true or false.
  • #number – The actual value must be a number.
  • #string – The actual value must be a string.
  • #uuid – The actual (string) value must conform to the UUID-Format (Universally Unique Identifier).
  • #regex – The actual (string) value must match the regular expression .

A detailed description of the Karate DSL can be found on this GitHub page.

Feature: Receive message
* url baseUrl
Scenario: Receive a single message
Given path '/news'
When method GET
Then status 200
And match $ == {message:'#notnull', id:'#number'}

Our project structure is now:

Karate directory structure

Karate directory structure

Running tests

The tests can then be run as usual using Maven:

mvn test


The Karate framework is a practical and useful tool for testing complex web services making life easier for the developer. Through easy-to-read tests, Karate allows you to focus on the functionality. And yes, it's fun! ? Here are some additional resources: the Karate project on GitHub, Writing BDD-style web service tests with Karate and Java and Testing a Java Spring Boot REST API with Karate.

Your partner for custom software projects

Are you planning a specific software project? Or are there certain processes in your company that cause many headaches? Does a system or interface slow down or get in your employees' or customers' way? Then talk to us! We have been developing software since 1996 and are looking forward to working with you to create your custom solution - with the highest quality and keeping costs as low as possible.

How agile software projects benefit customers
An introduction to Scrum
Reasons why you should test your new software intensively and at an early stage
Behavior-driven development and Bamboo: Visualizing Cucumber scenarios