Reputation: 6329
I am using SpringBoot with Cucumber. One particular test case fails when run together and passes when run individually. Other Scenarios in the same feature file seems to work fine. I tried the following links but they did not work.
Now I ran out of options. Please help.
Feature File:
Scenario Outline: To check if the service returns all the ids when more than 10000 Ids are returned by the query
Given sample request <payload>
When the new end point is invoked
Then the service returns <idCount> Ids
Examples:
| payload | idCount |
| sample-payload-1 | 17575 |
| sample-payload-2 | 4 |
| sample-payload-3 | 23535 |
| sample-payload-4 | 34535 |
Step Def File:
public class MyStepdefs extends AbstractStepsDefs {
@Autowired
MyController myController;
private String requestPayload;
private List<String> ids;
@Given("^sample request (.+)$")
public void sample_request_something(String payload) throws Throwable {
this.requestPayload = payload;
}
@When("^the new end point is invoked$")
public void the_new_end_point_is_invoked() throws Throwable {
String responseJSON = MyUtil.getPostResponseJSON(myController, "/ids", requestPayload);
responseJSON = responseJSON.replace("[", "").replace("]", "").replaceAll("\"", "");
ids = Arrays.asList(responseJSON.split(","));
}
@Then("^service returns list of available (.+)$")
public void service_returns_list_of_available_something(String ids) throws Throwable {
List<String> list = Arrays.asList(ids.split(","));
Assert.assertTrue(this.ids.containsAll(list));
}
@Then("^the service returns (.+) ids$")
public void the_service_returns_ids(String idCount) throws Throwable {
System.out.println("Actual Size:" + this.ids.size());
System.out.println("Expected Size:" + Integer.parseInt(idCount));
Assert.assertEquals(this.ids.size(), Integer.parseInt(idCount));
}
@After
@Before
public void cleanUp() {
ids = null;
}
}
Now I have run out of options. Please help.
UPDATE1:
The second then
block in the stepdef class fails. The sample-payload-1 and sample-payload-2 pass, but the other two fails. Even after changing the order of the sample-payloads the tests fail.
The error I get is an Assertion error as the size of the ids
list is not same. But when I run the same test Individually I don't get this error as the size matches.
Upvotes: 2
Views: 3732
Reputation: 6329
The issue was with the Instance variable I had used as a counter
to loop through a logic that fires the query and extracts the result. The first test sample [sample-payload-1
] will alone work fine as it has a fresh copy of the instance counter
. The subsequent samples will have only the altered counter
value.
This explains why the test cases passed when ran individually as there is not another test that would have used the altered counter
.
FIX: The fix was to reset the counter
back to 0
before I exit the implementation/service class so that the subsequent request will have a fresh counter
Lesson Learnt: Never rely on an instance variable if the scope
of the enclosing Class is Singleton
. The value will keep changing for every call to the method in the Class
Upvotes: 1