I should be able to use a ParamaterConverter to retrieve an entity from the database, e.g.
Given an employee called Bob
When I give Bob a pay rise of 10,000 pounds
Then Bob forgets about his embarrassing law suit
@Given("an employee called $name")
public void createEmployee(String name) {
new EmployeeBuilder().name(name).buildAndSave();
}
@When("I give $employee a pay rise of $amount pounds")
public void givePayRise(Employee employee, Integer amount) {
employee.increaseSalary(amount);
}
public class EmployeeConverter implements ParamterConverter {
EmployeeRepository repo;
public Object convertValue(String value, Type type) {
return repo.findByFirstName(value)
}
}
The parameter converter would be simple to implement, but currently won't work without shenanigans because the step arguments are converted when the step is created, and all the steps in a scenario are created up front, before any of them are executed. This means that at the time the parameter converter attempts to retrieve the entity from the database it hasn't been created.
To get around the problem I've created a LazyCandidateStep class, which only creates the Step in the perform or doNotPerform methods. This isn't ideals however because I also have to override Steps.createCandidateStep. I've attached my workaround as an example.
Please find attached a patch containing test cases and one possible solution. If you decide it's worth incorporating I think the attached would benefit from extracting the ParameterConverter interface and anonymous Steps class into their own files.
Another solution would to be always defer parameter conversion, but there's chance this would create problems if someone, somewhere relied on the conversion occurring before the scenario had run.