Consider the following cucumber scenario:-
Scenario: Test payment
Given I login to terminal
When POS token is generated asynchronously
Then user generates mobile token
And payment is successful
The step "POS token is generated asynchronously" needs to execute asynchronously and should not block the execution of downstream steps after it. I was able to get it done with FutureTask in Java. However in case of failures I am not able to assert the failures. Below is the code snippet
@When("^POS token is generated asynchronously$")
public void gs_Consumer() throws Throwable {
HashMap<String, Object> m = DataContainer.getDataMap();
ExecutorService executor = Executors.newFixedThreadPool(2);
FutureTask<Object> futureTask1 = null;
futureTask1 = new FutureTask<Object>(new Callable<Object>() {
public Object call() throws Exception {
DataContainer.setDataMap(m);
try {
retrieve_consumer_information();
} catch (Throwable e) {
DataContainer.getDataMap().put("exception", e);
throw new Exception(e);
}
return null;
}
});
executor.execute(futureTask1);
DataContainer.getDataMap().put("response", futureTask1);
// Shutdown the ExecutorService
executor.shutdown();
}
Then I get the response in the After method since I cannot do a futureTask1.get() inside this method as it will block the execution from executing the other downstream steps.
public void afterAsynchMethod() {
try {
((FutureTask<Object>) DataContainer.getDataMap().get("response")).get();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
Assert.fail(e.getMessage());
}
}
Now if the exception happens in the After method the scenario still is not reflected as a failed scenario. How do I fail the scenario in this case or any other ways of doing this?