2

I need to compare numbers which is greater than or lesser than count in cucumber test. How should I write scenario for this to do it in effective manner?

Scenario: Search and Compare count value.
Then I Search and Verify "user" count  is "less than" 3
Then I Search and Verify "user" count  is "greater than" 3

From an above scenarios How should I pass comparator operator and get from java stepdefs.

M A
  • 71,713
  • 13
  • 134
  • 174
Pez
  • 1,091
  • 2
  • 14
  • 34

1 Answers1

3

Instead of relying on a comparator, use Hamcrest lessThan and greaterThan matchers. In your step definition:

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

...

if(condition.equals("less than")) {
    assertThat(val, lessThan(count));
} else if(condition.equals("greater than")) {
    assertThat(val, greaterThan(count));
}

You can enrich that to include all possible conditions:

switch(condition.toLowerCase()) {

   case "less than":
   case "<":
       assertThat(val, lessThan(count));
       break;

   case "less than or equal to":
   case "<=":
       assertThat(val, lessThanOrEqualTo(count));
       break;

   case "greater than":
   case ">":
       assertThat(val, greaterThan(count));
       break;

   case "greater than or equal to":
   case ">=":
       assertThat(val, greaterThanOrEqualTo(count));
       break;

   ...
}

Note that you will need to depend on the Hamcrest library in your project (in case it is not already a dependency).

M A
  • 71,713
  • 13
  • 134
  • 174