Hello, a few months ago, Ali Aktolun briefly explained how to use Karate Framework for web service testing. Here’s the article. You need to be familiar with Karate in order to understand the Calling Custome Java Code in Karate API Tests tutorial.

The most important feature of Karate isno coding”. But there are cases where you need to take custom actions like saving a response to a file, file reading or writing, etc. In this tutorial, I’ll explain how to use Java codes in Karate API tests and Karate Projects.

My use case is testing web services which require authentication. My first web service does the authentication and creates a token, then all other services use that token. But generated token belongs to “Authentication test” context. When I execute the next web service, this token is not recognized as it belongs to previous test context. So what should I do?

  • I can use Karate’s built-in call method. This method will call “Authentication Service” before execution of any other scenarios. So I will have multiple web service calls which will increase execution time.
  • I can save generated token in a file. Then I read this token before other scenario’s execution. By this way, I will run the “Authentication Service” once.

Maybe there are other smart solutions to do that but this is the second one is the one I come up with to reduce the execution time for Java Code in Karate API Tests.

Implementing Java Code

Let’s start from Java Code that writes to and reads from a properties file. This code simply writes our token to config.properties file by using write method. Then you can read the value by using read method.

Write method takes a Map<String, Object> object as an input. I can send a key and its corresponding value as parameters by having this object. In case you want to send one String value, you can change your method signature as below. Implementation depends on you.

Read method only read from the config file by using a key value. As I only send one String, method signature is different than the previous one.

Calling JAVA Code in Karate API Tests

Here’s my test code that creates a token from the authentication server.

I need to call write method in this scenario to store generated token into a file.

Defining File Write Functionality

This command creates an object with doStorage name that you will use in your Karate DSL test. Blue ones are the name of my Java file. The green one is the package name of my Java class. As you can see we create a variable dS then call write method. This is the method name that we have in Java class.

Calling File Write Functionality

I add the below line to my scenario.

call is a built-in component of Karate framework. doStorage is the variable that we created earlier. {‘key’: #(token)} is the input that we provide.

#(token) allows you to get the value of the token variable.

So final version of our test is below.

Defining File Read Functionality into Karate Test

This test scenario fetches some data from the server. We need to provide a token to execute this scenario. Let’s do that.

Once again we create a doStorage object calling our Java class’ read method.

This line creates an object with doStorage name that you will use in your Karate DSL test. Blue ones are the name of my Java file. The green one is the package name of my Java class. As you can see we create a variable dS then call read method. This is the method name that we have in Java class.

Calling File Read Functionality

Then we call our this object with an input. As we stored our token with “key” value, we pass “key” parameter to our method.

call is a built-in component of Karate framework. doStorage is the variable that we created earlier. The key is the input that we provide to that function. Then our test scenarios change as below:

As you can see, there are no limits to what you can achieve with Karate. You can find the project here: https://github.com/swtestacademy/Karate-API-Test-with-Java-Code

Thanks.
Canberk Akduygu