Simple Cucumber example

Cucumber is a testing framework used to test another software.
The tests are written in  BDD approach (behavior driven development).

A Simple Example:

public class Book {

private String name;
private String auther;

public Book(String name, String auther) {
this.name = name;
this.auther = auther;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
}

public class Library {

private List bookStore = new ArrayList();

public void addBook(Book book) {
bookStore.add(book);
}
public List findBooks(String auther) {
List newList = new ArrayList();
for (Book book : bookStore) {
if (book.getAuther().equals(auther)) {
newList.add(book);
}
}
return newList;
}
}

We have a book store. We will test the search feature in our book store.

Now we will write the behavior in a feature file.


Feature:Book Search
To allow customers to search books quickly
Scenario:Search book by author
Given A book with title 'book1' is written by author 'A'
And A book with title 'book2' is written by author 'B'
And A book with title 'book3' is written by author 'C' 
When the customer search for author 'A'
Then '1' book should be found
And The book should have author 'A'


Given A book with title 'b1' is written by author 'A1'
And A book with title 'b2' is written by author 'A1'
And A book with title 'b3' is written by author 'A3' 
When the customer search for author 'A1'
Then '2' book should be found
And The book should have author 'A1' 

Now we will write step which uses the feature file and test the feature.

public class BookSearchSteps {

Library library = new Library();
List result = new ArrayList();

@Given("A book with title '(.+)' is written by author '(.+)'")
public void addNewBook(String title,String auther){
library.addBook(new Book(title, auther));
}
@When("^the customer search for author '(.+)'")
public void addSearchCriteria(String auther){
result = library.findBooks(auther);
}
@Then("'(.+)' book should be found$")
public void verifyAmountOfBook(int booksFound){
MatcherAssert.assertThat(result.size(), IsEqual.equalTo(booksFound));
}
@Then("The book should have author '(.+)'")
public void verifyAuther(String auther){
for (Book book:result){
MatcherAssert.assertThat(book.getAuther(), IsEqual.equalTo(auther));
}
}
}

For running the test case. We can not run the step file directly.
We need to have another file to run the test.

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty"},features="\\search_book.feature")
public class BookSearchTest {
}


Lets take another example.
When you have to test multiple inputs, then in this approach you need to write multiple given when then.
In that case we can use scenario outline. For running scenario you must use 1.2.4

public class AppleStore {

private int noOfApple;

public AppleStore(int noOfApple){
this.noOfApple = noOfApple;
}
public int eatApple(int howManyToEat){
return noOfApple-howManyToEat;
}

}

AppleStore.feature
Feature:Apple Store check
Scenario Outline: Eating
Given There are "" apples
When I ate "" apples
Then There should be "" left

Examples:
|total |eat |left  |
|  2    |   1   | 1    |
|  20   |   2   | 18   |
|  9    |   2   | 7    |
|  5    |   0   | 5    |
|  3    |   2   | 1    |


import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class AppleStoreDefinition {
AppleStore appleStore;
int remaining;

@Given("^There are \"(.+)\" apples$")
public void init(int total) {
appleStore = new AppleStore(total);
}
@When("^I ate \"(.+)\" apples$")
public void add(int noOfApplesToEat) {
remaining = appleStore.eatApple(noOfApplesToEat);
}
@Then("^There should be \"(.+)\" left$")
public void verify(int exceptedResult) {
MatcherAssert.assertThat(remaining, IsEqual.equalTo(exceptedResult));
}
}

In this example no need to write multiple given when then.




No comments:

Post a Comment