Cucumber JVM: How do I use a double as an input value?

11,525

Solution 1

Simple (.+) should work

Given I have a floating point 1.2345 number

@Given("^I have a floating point (.+) number$")
public void I_have_a_floating_point_number(double arg) throws Throwable { 
    ... 
}

Solution 2

My own preference is to specify digits either side of a dot, something like...

@Given("^the floating point value of (\\d+.\\d+)$")
public void theFloatingPointValueOf(double arg) {
    // assert something
}

and as you mentioned floating point inputs plural, I might handle the multiple inputs with an outline like...

Scenario Outline: handling lots of floating point inputs
    Given the floating point value of <floatingPoint>
    When something happens
    Then some outcome

    Examples:
        | floatingPoint |
        | 2.0           |
        | 2.4           |
        | 5.8           |
        | 3.2           |

And it will run a scenario per floating point input

Solution 3

I use the form

 @When("^We change the zone of the alert to \\(([0-9\\.]+),([0-9\\.]+)\\) with a radius of (\\d+) meters.$")
 public void we_change_the_zone_of_the_alert_to_with_a_radius_of_meters(double latitude, double longitude, int radius)

so [0-9.]+ make the deal :)

Take care of the local of your cucumber. If you're using language:fr for example, number are using , for delimiter.

Share:
11,525
user321605
Author by

user321605

Updated on June 07, 2022

Comments

  • user321605
    user321605 almost 2 years

    For a Behavior test that I'm trying to write, I require inputs that are floating point. How do I set up my gherkin string to look for these values?

  • Eriky R. Kashivagui
    Eriky R. Kashivagui almost 8 years
    That was not the matter of the question
  • Marc von Renteln
    Marc von Renteln over 6 years
    The language makes the difference! It's the same for language: de. Thanks!