In this post I will like to work out a example in Cucumber which takes on a very familiar setup these days in any kind of software organisation: Acceptance testing of a RESTful web service.
For anyone who has been following my posts, I tend to make use of online services in my testing examples. This time is no different and the goal is to:
Write set of acceptance tests for http://imdbapi.org's RESTful API
And I am going to use Cucumber-JVM achieve the objective. As the name suggest imdbapi.org exposes IMDB data via a RESTful api and we are going to search for movies! Try out following to understand the scope of this testing:
All or most of the fun in Cucumber is in writing the .feature file which is in Gherkin which is the language which many BDD tools like Cucumber understands.
Lets start looking my .feature file. It contains 2 Scenarios. First one is a simple scenario which represents 1 test case, while the second one is actually a set of test cases with different input and expected output. There are a lot of other ways which one can represent their set of acceptance test cases in Gherkin but for sake of example I have used the following and this file should be self explanatory which is what Gherkin is good at.
Now create a simple RunImdbTest.java file which will be used to run test cases in .features file via JUnit.
At this point you can run 'mvn test' and let Cucumber print out the snippet of code that you need to include in your Step Definition file where basically the conversion between plain english test statements happen to actual java code behind. In our case this file is ImdbSteps.java
This helper class has to logic to http get the movie titles and convert the response in JSON objects for comparison in Step Definition class.
Finally our test cases are ready and we can run them using 'mvn test' command. This run will also create the green cucumber test report which all product managers like to see.
All project files are uploaded on github.
For anyone who has been following my posts, I tend to make use of online services in my testing examples. This time is no different and the goal is to:
Write set of acceptance tests for http://imdbapi.org's RESTful API
And I am going to use Cucumber-JVM achieve the objective. As the name suggest imdbapi.org exposes IMDB data via a RESTful api and we are going to search for movies! Try out following to understand the scope of this testing:
Setup Java Project with Maven
Put all dependencies in pom.xml
Since I have already coded up the test case my directory structure is looking like following:All or most of the fun in Cucumber is in writing the .feature file which is in Gherkin which is the language which many BDD tools like Cucumber understands.
Lets start looking my .feature file. It contains 2 Scenarios. First one is a simple scenario which represents 1 test case, while the second one is actually a set of test cases with different input and expected output. There are a lot of other ways which one can represent their set of acceptance test cases in Gherkin but for sake of example I have used the following and this file should be self explanatory which is what Gherkin is good at.
Now create a simple RunImdbTest.java file which will be used to run test cases in .features file via JUnit.
At this point you can run 'mvn test' and let Cucumber print out the snippet of code that you need to include in your Step Definition file where basically the conversion between plain english test statements happen to actual java code behind. In our case this file is ImdbSteps.java
ImdbStep.java
This Step Definition file contains a set of annotations with capture regular expressions that drive the whole automation between plain english statements and java code behind to do the job.
JsonReader.java
This helper class has to logic to http get the movie titles and convert the response in JSON objects for comparison in Step Definition class.
Finally our test cases are ready and we can run them using 'mvn test' command. This run will also create the green cucumber test report which all product managers like to see.
All project files are uploaded on github.
I have one clarification..
ReplyDeleteThanks for sharing this example. but when I tried to use that example for one of my project, but its always working, even though if my REST API is in down mode also....please let me know why its not throwing any error message...
You can validate response code first, like if it's 200 then only read and parse the response json otherwise fail with appropriate message.
ReplyDelete"Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
ReplyDelete"