BDD framework with Rest -assured + cucumber using scala and sbt

Reading Time: 4 minutes

Hi all , here we are going to develop a BDD framework for automating our Rest API’s with the help of rest assured and cucumber using scala and sbt build tool.

REST assured DSL already provides a BDD style writing of tests in the Given , When and Then format , but still if you want to know what scenarios are covered , you still have to dig down into the api tests and read the code.

With the help of cucumber , Rest assured api tests are refactored so that scenarios can be read easily without looking at underlying code.

Steps to follow for setting up the framework :-

  1. Install IDE : I have used Intellij as an IDE for my Automation Test Suite development. You can download it from https://www.jetbrains.com/idea/download
  2. Enable plugins : 
    1. Open Intellij
    2. Go to file > settings > plugins
    3. Search for Scala , Gherkin and cucumber with Scala plugin and install.
  3. Create a new Scala project with sbt (Scala build tool).
  4. Add following dependencies for sbt under build.sbt file :
name := "Restassured_with_cucumber"

version := "0.1"

scalaVersion := "2.13.4"

// https://mvnrepository.com/artifact/io.rest-assured/rest-assured
libraryDependencies += "io.rest-assured" % "rest-assured" % "4.3.3"

// https://mvnrepository.com/artifact/io.cucumber/cucumber-scala
libraryDependencies += "io.cucumber" %% "cucumber-scala_2.11" % "6.9.1"

// https://mvnrepository.com/artifact/io.cucumber/cucumber-junit
libraryDependencies += "io.cucumber" % "cucumber-junit" % "6.9.1"

// https://mvnrepository.com/artifact/junit/junit
libraryDependencies += "junit" % "junit" % "4.13.1"

In cucumber, we have:-

Feature files

In feature file, we have to define all the features which we are going to test. Under feature file , we have to define feature , Scenario , Given , When , Then and And .

  • Feature :- The feature to be tested  (Test Suite) e.g :- login feature
  • Scenario :- The scenario we are going to test for defined feature (Test Case) e.g:- login feature with valid inputs or invalid inputs.
  • Given :- Details which we have like Email Id and Password.
  • When :- The action which needs to be performed for testing a defined scenario e.g :- On clicking a sign in button
  • Then and And :- used for validating the results.
StepDefinition files  

It contains implementation for all the scenarios with annotations given , when , then and And.

Runner class

It is used to run our Junit tests.

Let’s take an example of  dummy api for creating and fetching employee data for our framework.

curl –location –request POST ‘http://dummy.restapiexample.com/api/v1/create

curl –location –request GET ‘http://dummy.restapiexample.com/api/v1/employee/4948

  1. First , create a package “features” under src/test/scala
  2. Now, create a file with feature_name.feature under package features.
Feature file
  1. Create another package “stepDefinitions” under src/test/scala with a scala class which will contain the implementation for defined features present in your feature file.
package stepDefinitions

import io.cucumber.scala.{EN, ScalaDsl}
import io.restassured.RestAssured.{`given`, when}
import io.restassured.response.Response
import org.junit.Assert.assertEquals

class FeatureImplementation extends ScalaDsl with EN {

  private val baseURI = "http://dummy.restapiexample.com"
  private val endpoint = "/api/v1/"
  var response : Response = null
  var id : Int = 4948

  Given("""Add employee payload""") { () =>
  given().body("\"name\":\"ABC\",\"salary\":\"20000\",\"age\":\"25\"")
  }

When("""user calls add employee api with post http request""") { () =>
      response = when().post(baseURI + endpoint + "create") .`then`().log().all().extract().response()

  }

Then("""API call is success with status code 200""") { () =>
    assertEquals(response.getStatusCode(),200)
  }

When("""User calls get employee api with get http request""") { () =>
response = when().get(baseURI + endpoint + "employee/" + id ) .`then`().log().all().extract().response()
  }
Then("""Validate response""") { () =>
    var result = response.getBody().toString
    println(result)
    assertEquals(response.getStatusCode(),200)
  }
}
  1. Lastly, you have to create a third package naming cucumber.Options with a scala class under it , which will perform as a runner class.
package cucumber.Options

import io.cucumber.junit.{Cucumber, CucumberOptions}
import org.junit.runner.RunWith

@RunWith(classOf[Cucumber])
@CucumberOptions(features = Array("classpath:features/test.feature") , glue = Array("stepDefinitions"))
class TestRunner {


}
  1. Run the runner class as a junit test.
  2. The result will appear like this :-

Now let’s see how all this works ,

In runner class we have defined path for our features file and stepdefinitions file under @Cucumber.Options annotation. When we run our runner class as a junit test , first it reads our feature from feature file and then invoke its corresponding implementation from stepDefinitions file and returns the output in the console.

In this blog , I have explained that how can we setup a rest assured framework with cucumber by taking a simple example of GET and POST request. In my next blog , I will be coming with a complex problem and will be explaining parameterization with multiple data sets.


Knoldus-blog-footer-image