首页 > 解决方案 > 实数的步进文件法

问题描述

特征:添加小数特征

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

如何匹配所有正负实数?

标签: javagherkin

解决方案


感谢 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 的更长版本。所以如果浮动有效,为什么不加倍


推荐阅读