Purpose
- Focused guide to writing BDD-style API tests with Karate.
- Includes setup, patterns, data-driven tests, mocks, and interview questions.
Table of Contents
- Setup (Maven plugin)
- Basic feature syntax
- Configuration and karate-config.js
- Data-driven tests (Scenario Outline, Examples)
- Calling Java code and JS functions
- Mocking (API stubs) and mocks in CI
- Assertions, matchers, and JSON path
- Parallel execution and reporting
- Common pitfalls
- Interview questions and exercises
Add Karate plugin to your pom:
<plugin>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>Example feature file get-user.feature:
Feature: User API
Scenario: Get user by id
Given url 'https://reqres.in/api/users/2'
When method get
Then status 200
And match $.data.first_name == 'Janet'- Central place for environment config, baseUrl, and variables. Example:
function() {
var config = {};
config.baseUrl = 'https://reqres.in';
return config;
}Scenario Outline and Examples
Scenario Outline: Check user exists
Given url baseUrl + '/api/users/<id>'
When method get
Then status 200
And match $.data.id == <id>
Examples:
| id |
| 1 |
| 2 |Or use call with data files:
* def users = read('users.json')
* for(var u in users) karate.log(u.id)- You can call Java utility classes from Karate.
- You can write reusable JS functions in
karate-config.jsor separate JS files and call them.
Example calling JS function:
* def gen = function(x){ return x + '-id' }
* def id = gen('user')Karate can run as a mock server to simulate external services.
- Create a
mock.featurethat listens and returns canned responses. - Useful for CI to avoid external flakiness.
Common matchers
match response == { id: 1, name: 'John' }match response.data contains { email: '#regex .+@.+\..+' }match response.data[0].id == 1match response contains { data: '#[]' }// list assertion
Schema validation: use JSON schema files and match response == read('schema.json') patterns.
- Karate plugin runs tests in parallel by default for faster feedback.
- Use
karate.output.diror plugin config to collect HTML reports. - In CI, publish the generated HTML or convert to JUnit XML for GitHub Actions.
- Overusing
karate.login large runs — excessive logs slow performance. - Relying on external services without mocks — flakiness.
- Mixing too much Java code into features — prefer readable feature files.
Q: What is Karate?
A: Karate is a BDD-style test framework for API, UI, and performance testing that uses Gherkin syntax and includes HTTP client and assertion support.
Q: How do you do data-driven tests in Karate?
A: Use Scenario Outline with Examples, or read data files (JSON/CSV) and iterate.
Q: How to mock an external service?
A: Use Karate's mock server feature: create a feature that listens and returns canned responses during tests.
Q: How to call Java code from Karate?
A: Place Java utility classes on the classpath and call them using java interop in feature files.
- Create a Karate feature that POSTs a new user, asserts 201, then GETs the user and verifies fields.
- Create a mock for an external payment provider and write tests that consume the mock.
- Use a JSON schema to validate a complex response structure.
Keep this page as your interview pocket reference for Karate.