ivanursul

Software engineer. A fan of CI/CD. Blogger.

Testing RESTful CRUD Web Service with Gatling Tool

If you follow the right way of designing your web app, and choosed RESTful architecture, then it should be easy for you to test your appplication. By the REST style, you should implement CRUD operations for each entity in your application. What does it mean:

You should be able to Create,Read, Update, Delete(CRUD) your entities. You should provide statuses for each operations. For example, for Create operation you should have 201 status in case of succesful operation. You application must have good exception handling. For example, if you will make GET request for retrieving some entity called person with id - 1, and there will be no such record in your storage with this id, then you need to tell your user, that there is no such person, and set status to 404(Not found) So, given that you will keep in mind all information above and will design your app with such instructions.

Now it’s time to write some tests. Which framework you should choose?

I choosed Gatling framework. Reasons:

Configuration

As I mentioned above, I will use maven for building my app.

So, I will create a separate module for running my integration tests.

Gatling integration module

with jar packaging

Setup you pom.xml like in below:

Scala IDE

To write tests with gatling, you need to have IDE with scala support.I choosed Scala ready IDE. You can download it here. After scala installation, Right click on project -> Configure - > Add Scala Nature.

Then create Source Folder in src/test/scala.

Now you are ready to write tests.

Creating first test

To have scalable structure, create a Scala class - org.lnu.is.integration.IntegrationTest.

This class will be entry point for all future tests.

Now let’s create integration test for person(just an example), note that it should be object, not a class - it’s a special singleton construction in scala.

Edit your IntegrationTest class

Adding resources

Note, that all data(json templates, etc..) are store in data folder

data/person/post.json:

data/person/put.json:

Describing scenario

The idea of scenario is:

Generating custom values

There are cases, where you need to generate unique value for each request.

For example, some person identifier needs to be unique.

For solving such issue I use session configuration:

.exec(session => {
  session
    .set("idnum", UUID.randomUUID())}) and in json file I just make a placeholder:

“identifier”: “${idnum}”,

That seems to be all, that you need .

Running test

Start your server, open terminal, go to your integration module folder and execute

mvn clean install

Watch the results.