我一直在学习使用Jakub Czeczótka's great blog的Cucumber for Java。但是他在描述数据表的时候把我搞糊涂了。
我用加法、减法、乘法和除法做了一个计算器类。每种方法的单独测试都通过了。当我尝试使用一个表时,我从小黄瓜那里得到了一个不匹配的错误:
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
{
}
所以我的桌子似乎还没到小黄瓜那里。我假设我的地图也有问题,但当我解决了arity问题时,我会解决这个问题的。
我需要做些什么来解决这种不匹配的情况?
发布于 2018-06-08 11:13:55
在功能文件中没有该场景的这一步的DataTable。不需要在此步骤定义中添加DataTable参数。
这个例子似乎强调了ScenarioOutline而不是DataTable的使用。
到目前为止,ScenarioOutline定义了一组类似于场景的步骤。现在可以使用examples表中的数据重复执行这些步骤。此表的每一行对应于所述步骤的一次执行。因此,如果你有3行,就会有3次运行。
另一方面,DataTable用于将数据作为一种列表发送到场景的特定步骤。比如一份购物清单。这只运行一次。虽然它也可以在ScenarioOutline中使用,因此允许您使用变量列表
发布于 2018-06-11 19:56:16
ArityMismatch表示您没有传递正确数量的参数。例如:
@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
该步骤使用2个参数(分子和除数),但该方法没有指定这些参数。
改为使用这样的内容:@When( "^I除以$“) public void iDivideNumeratorByDivisor(int num,int div) {
https://stackoverflow.com/questions/50750682
复制相似问题