我一直在使用Jakub Czeczótka's great blog学习Cucumber for Java。但在描述数据表时他失去了我 我用加法,减法,乘法和除法制作了一个计算器类。每种方法的单独测试都通过了。当我尝试使用表时,我从Gherkin得到了一个arity不匹配错误:
cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'com.test.bdd.steps.CalculatorSteps.iWantToPerformADataTableCalculation(DataTable) in file: .../Code/Java/CucumberTest/target/test-classes/' with pattern [^I want to perform a data table calculation$] is declared with 1 parameters. However, the gherkin step has 0 arguments [].
以下是相关文件和路径:
src/main/java/com/test/bdd/calculator/
package com.test.bdd.calculator;
public class Calculator
{
private int result;
public void divide( int a, int b )
{
result = a / b;
}
public int getResult()
{
return result;
}
}
src/test/resources/cucumber/
Scenario: Division
Given I want to perform a calculation
When I divide 14 by 2
Then the result should be 7
Scenario Outline: Division Data Table
Given I want to perform a data table calculation
When I divide <Numerator> by <Divisor>
Then the result should be <Result>
Examples:
| Numerator | Divisor | Result |
| 100 | 2 | 50 |
| 100 | 4 | 25 |
| 1000 | 200 | 5 |
src/test/java/com/test/bdd/steps/
package com.test.bdd.steps;
public class CalculatorSteps
{
private Calculator calculator;
@Before
public void setUp()
{
calculator = new Calculator();
}
@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
// Write code here that turns the phrase above into concrete actions
calculator.divide( 4, 3 );
}
@Then( "^the result should be <Result>$" )
public void theResultShouldBeResult()
{
// Write code here that turns the phrase above into concrete actions
assertEquals( 1, calculator.getResult() );
}
@Given( "^I want to perform a data table calculation$" )
public void iWantToPerformADataTableCalculation( DataTable table )
{
if( table != null )
{
for ( Map<String, Integer> map : table.asMaps( String.class, Integer.class ) )
{
Integer numerator = map.get( "Numerator" );
Integer divisor = map.get( "Divisor" );
Integer result = map.get( "Result" );
System.out.println( format( "Dividing %d by %d yields %d", numerator, divisor, result ) );
}
}
}
}
package com.test.bdd.runner;
@RunWith( Cucumber.class )
@CucumberOptions(
glue = "com.test.bdd.steps",
features = "classpath:cucumber/calculator.feature" )
public class RunCalculatorTests
{
}
所以我的桌子似乎没有到达Gherkin。我认为我的地图也存在问题,但是当我超越arity问题时会处理这个问题 我需要做些什么才能解决这种不匹配问题?
答案 0 :(得分:1)
功能文件中此方案的此步骤没有DataTable。不需要在此stepdefinition中添加DataTable参数。
这个例子似乎突出了ScenarioOutline的使用,而不是DataTable的使用。
ScenarioOutline定义了一组步骤,到目前为止类似于一个场景。现在可以使用示例表中的数据重复执行这些步骤。该表的每一行对应于所述步骤的一次执行。所以如果你有3行就会有3次运行。
另一方面,DataTable用于将数据作为一种列表发送到场景的特定步骤。就像购物清单一样。这只运行一次。虽然它也可以在ScenarioOutline中使用,因此允许你使用变量列表
答案 1 :(得分:1)
ArityMismatch意味着您没有传递正确数量的参数。 例如:
@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
该步骤使用2个参数(分子和除数),但该方法未指定这些参数。
使用类似的东西: @When(“^我除以$”) public void iDivideNumeratorByDivisor(int num,int div) {