Karate is the open source tool to combine API test automation, mockery, performance testing and even UI automation into a single framework. The BDD syntax with Cucumber is launguage neutral, and easy for even non-programmers. Powerful JSON & XML declarations are built-in, and you can run tests in parallel for speed.
API tests are written using BDD Gherkin syntax. But unlike most BDD frameworks (Cucumber, JBehave, SpecFlow), you don’t need to write step definitions. Karate has already created all the step definitions you need to get started testing APIs.
Prerequisites
To integrate Karate in our project using Maven as a build tool, we only need to add the following two dependencies to pom.xml:In case you are working with Junit 4 , use the below snippet
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>0.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.9.5</version>
<scope>test</scope>
</dependency>
In case you are working with Junit 5 , use the below snippet
<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-junit5</artifactId>
<version>0.9.5</version>
<scope>test</scope>
</dependency>
Implementing Karate Tests
We’re now ready to write our first tests. Tests consist of a Java class that might be recognized by the designated test framework used and a feature file that describes our interaction with the service under test.
Directory Structure
Karate tests scripts use the *.feature extension. The naming convention recommends putting the features next to the *.java files. We can configure Maven to follow the recommended convention by editing pom.xml:
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>
On the final account, the naming convention follows this pattern:

Feature Files
Karate features are written in a DSL that we’ll be covering in the following examples.Our features are generally stored in src/test/java/ so that feature files and Java tests are matched by their name and package structure.
JUnit Integration
All we need to do to integrate JUnit is to create a test class addressing our corresponding JUnit runner so that a minimal approach could look like the following class.
If you have added Junit 4 dependency in the POM.xml ,use the below runner
package testPackage;
import org.junit.runner.RunWith;
import com.intuit.karate.junit4.Karate;
import com.intuit.karate.KarateOptions;
@RunWith(Karate.class)
@KarateOptions(features = "classpath:employer/createEmployer.feature")
public class SampleTest {
}
If you have added Junit 5 dependency in the POM.xml ,use the below runner class
package karate;
import com.intuit.karate.junit5.Karate;
classSampleTest{
@Karate.TestKaratetestSample(){
returnKarate.run("sample").relativeTo(getClass());
}
@Karate.TestKaratetestTags(){
returnKarate.run("tags").tags("@second").relativeTo(getClass());
}
@Karate.TestKaratetestFullPath(){
returnKarate.run("classpath:karate/tags.feature").tags("@first");}
}