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

4

7

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?

Sowers answered 15/2, 2013 at 19:37 Comment(0)
T
11

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 { 
    ... 
}
Torso answered 27/2, 2013 at 1:38 Comment(0)
M
5

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

Moltke answered 2/11, 2015 at 11:46 Comment(0)
H
2

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.

Hilaria answered 15/9, 2014 at 14:40 Comment(1)
The language makes the difference! It's the same for language: de. Thanks!Arica
P
-1

You should escape the float number with (\\d+)

Example

Given I have a floating point 1.2345 number

@Given("^I have a floating point (\\d+) number$")
public void I_have_a_floating_point(double arg){

}
Prewar answered 3/12, 2013 at 13:12 Comment(1)
That was not the matter of the questionGravy

© 2022 - 2024 — McMap. All rights reserved.