JBehave
  1. JBehave
  2. JBEHAVE-244

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
        8 kB
        Stephen Cresswell
      2. BaseSteps.groovy
        1 kB
        Stephen Cresswell
      3. LazyCandidateStep.groovy
        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.
        Stephen Cresswell made changes -
        Field Original Value New Value
        Attachment Added_support_for_deferred_parameter_conversion.patch [ 47943 ]
        Mauro Talevi made changes -
        Fix Version/s 3.0 [ 16302 ]
        Fix Version/s 2.6 [ 16238 ]
        Mauro Talevi made changes -
        Fix Version/s 3.1 [ 16511 ]
        Fix Version/s 3.0 [ 16302 ]
        Mauro Talevi made changes -
        Fix Version/s 3.0 [ 16302 ]
        Fix Version/s 3.1 [ 16511 ]
        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 .
        Mauro Talevi made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Duplicate [ 3 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Stephen Cresswell
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: