实数的步骤文件方法

时间:2019-03-20 12:28:56

标签: java gherkin

功能:十进制功能的添加

Scenario: Add two positive decimal numbers
Given I am on the demo page
When I add the numbers 2.25 and 3.25
Then the result is 5.5

我认为when方法应该类似于

 @When("^I add the numbers (-?\\d+\\.?\\d*) and (-?\\d+\\.?\\d*)$")
public void i_add_the_numbers_and(double x, double y) throws Throwable {
   demoPage.addNumbers(x, y);
}

@Then("^the result is (-?\\d+\\.?\\d*)$")
public void the_result_is(double sum) throws Throwable {
    assertEquals(demoPage.getCalculatorResults(), sum);
}
}

问题是数字正则表达式与实数不匹配。例如-1.2或5.4或-23.234

如何匹配所有正负实数?

1 个答案:

答案 0 :(得分:0)

感谢NeplatnyUdaj

以下作品

/**
 * @param x
 * @param y
 * @throws Throwable
 */
@When("^I add the numbers (-?\\d+\\.?\\d*) and (-?\\d+\\.?\\d*)$")
public void i_add_the_numbers_and(float x, float y) throws Throwable {
  demoPage.addNumbers(x, y);
}

 /**
 * @param sum
 * @throws Throwable
 */
@Then("^the result is (-?\\d+\\.?\\d*)$")
public void the_result_is(float sum) throws Throwable {
    assertEquals(demoPage.getCalculatorResults(), sum);
}

那么为什么使用float或BigDecimal会使该方法起作用?我的理解是double是float的较长版本。所以如果float有效,为什么不会加倍