Karate framework — approach for testing your API

Oleg Mazurashu
Everon Engineering
Published in
4 min readJan 28, 2018

I am starting this post with a conclusion that the testing as a process is one of the keys of success of your application. If you want to have stable application which can fit your business requirements you need to spend a lot of time on testing and make sure that every business feature does expected things. And sometimes the whole testing process can take a lot of time. That’s why we always use tools for such kind of tasks. They allow us to save time and do more within application.

As a developer I can admit that there are a lot of very interesting and useful libraries and frameworks on the market right now. Some of them are mature enough for using them during development,… some of them will rise and simplify our life in future.

Depending on your process you can have different types of tests in your application(s), for example (just few of them) unit, integration, acceptance, smoke, component, api tests, etc (sounds familiar?!). Do not be scared about this short list, you don’t need to use all of them right now and please be sure that you understood that different names don’t mean that you need to have different tests in your code ;).

As a part of cool development team I write different type of tests after finishing or even during implementation of any business feature, so it’s not a secret that we as a team are using different tools for different purposes. Our toolbox contains few interesting frameworks for doing testing of all our application layers. But that doesn’t prevent us from monitoring java ecosystem and checking if there is a new library or framework which can simplify our daily activities.

Few months ago I was doing research about java testing libraries which we can evaluate and apply in our development environment. So I started from browsing GitHub and discovered very interesting project — Karate which was new for me but mature enough for community. I wanted to try it so I spent some time and created few small projects which I was testing with Karate.

I wrote this post because I wanted to say — “Hello World” to Karate community and show how easy is to test your API part if you are using right tool.

Yes, Karate framework is a tool fo testing your RESTful API. I copied the text from description:

Karate is built on top of Cucumber-JVM, you can run tests and generate reports like any standard Java project. But instead of Java — you write tests in a language designed to make dealing with HTTP, JSON or XML — simple”.

Sounds goooooood! I was using cucumber-js for testing some time ago and I liked it. It was simple to use, flexible enough and provided a lot of functionality. I was expected to see similar concepts in Karate… and Karate provides even more.

Please leave the comment with message “I did it” if you read its Readme till the end like me… so I did it :)

To be honest, project readme file contains all necessary information for starting using framework properly. So I recommend to read it. From my experience when I finished reading overview of the framework I knew how to use it in my application. Thanks for that to team and contributors who made it simple enough to start using it without long learning curve.

For my sample project I was using Spring Initializr to generate my simple Spring Boot application. I have added also database support by using Spring Data JPA and implemented REST API by using Spring MVC.

Application is very simple. It contains one User entity with JPA repository which provides access to DB and single REST controller which does few operations:

GET all users

GET single user by id

POST create new user

DELETE user by id

For simplification I will not describe the functionality of the simple business logic instead I will show scripts which do the testing. Sample application is saved on GitHub and you always welcome to clone it.

So let’s start from simple scenario. GET request which will return the list of all existing users:

As you can see nothing complicated. Just very simple script. I just provided relative URL path, verified expected HTTP status code and response.

The next part is more complex, because scenario will include few requests.

I have to create new user by using POST request with body:

And again — simple script for this task. After this my DB will contain new user.

Next request is GET for returning user by Id. Script below demonstrates how I did this:

Here I was defining variable which holds Id of created user in previous test.

After that I want to do the GET of all users to check all of them:

As you can see it’s similar to previous one but with updated asserts.

And finally I want to DELETE created user. I already have user Id so it’s simple:

Wow. It was fun and easy to test endpoints.

Let’s see the result in my IDE:

Looks great!

As you can see Karate is very helpful for such kind of testing. It’s definitely worth to try it!

If these snippets are not enough please visit sample project GitHub page to check more details.

I implemented project which you can run locally and see how to use Karate for testing. Please follow steps described in Readme file to run tests.

If you are still with me (thanks!) then you can see that Karate provides very interesting approach for testing your API layer by writing simple scripts. For more details about usage please refer to its official GitHub page.

I hope it was useful for you to see the project in action.

By the way, if you read this post till this word you definitely want to follow our engineering blog, believe me — we just started :).

And yes, we are hiring ;)

--

--