JBehave

Step parameters should be converted just before the step executes (not when the scenario is parsed)

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Duplicate
  • Affects Version/s: 2.5
  • Fix Version/s: 3.0
  • Component/s: Core
  • Labels:
    None
  • Number of attachments :
    3

Description

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

EmployeeSteps.java
@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);
}
EmployeeConverter.java
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.

  1. Added_support_for_deferred_parameter_conversion.patch
    15/Mar/10 5:12 PM
    8 kB
    Stephen Cresswell
  2. BaseSteps.groovy
    12/Mar/10 4:03 AM
    1 kB
    Stephen Cresswell
  3. LazyCandidateStep.groovy
    12/Mar/10 4:03 AM
    2 kB
    Stephen Cresswell

Activity

Hide
Stephen Cresswell added a comment -

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.

Show
Stephen Cresswell added a comment - 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.
Hide
Mauro Talevi added a comment -

Fixed by JBEHAVE-263 and JBEHAVE-264.

Show
Mauro Talevi added a comment - Fixed by JBEHAVE-263 and JBEHAVE-264.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: