Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Java Cucumber数据表的Arity不匹配_Java_Cucumber - Fatal编程技术网

与Java Cucumber数据表的Arity不匹配

与Java Cucumber数据表的Arity不匹配,java,cucumber,Java,Cucumber,我一直在学习Cucumber for Java使用。但他在描述数据表时把我弄糊涂了。 我上了一堂加、减、乘、除的计算器课。每种方法的单独测试均通过。当我尝试使用表格时,我从Gherkin得到一个算术不匹配错误: cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'com.test.bdd.steps.CalculatorSteps.iWantToPerformADataTableCalculation(Data

我一直在学习Cucumber for Java使用。但他在描述数据表时把我弄糊涂了。
我上了一堂加、减、乘、除的计算器课。每种方法的单独测试均通过。当我尝试使用表格时,我从Gherkin得到一个算术不匹配错误:

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
{
}
src/main/java/com/test/bdd/calculator/
包com.test.bdd.calculator;
公共类计算器
{
私有int结果;
公共空分(整数a、整数b)
{
结果=a/b;
}
public int getResult()
{
返回结果;
}
}
src/测试/资源/黄瓜/
场景:分区
假设我想进行一次计算
当我把14除以2时
那么结果应该是7
场景概要:部门数据表
假设我想执行一个数据表计算
当我除以
那么结果应该是
示例:
|分子|除数|结果|
| 100       | 2       | 50     |
| 100       | 4       | 25     |
| 1000      | 200     | 5      |
src/test/java/com/test/bdd/steps/
包com.test.bdd.steps;
公共类计算器步骤
{
私人计算器;
@以前
公共作废设置()
{
计算器=新计算器();
}
@当(^I除以$)时
公共无效iDivideNumeratorByDivisor()
{
//在这里编写代码,将上面的短语转化为具体的行动
计算器。除法(4,3);
}
@然后(“^结果应为$”)
public void theresult shouldberesult()
{
//在这里编写代码,将上面的短语转化为具体的行动
assertEquals(1,calculator.getResult());
}
@给定(“^I要执行数据表计算$”)
public void iwanttoperformadatablecalculation(数据表)
{
如果(表!=null)
{
for(映射:table.asMaps(String.class,Integer.class))
{
整数分子=map.get(“分子”);
整数除数=map.get(“除数”);
整数结果=map.get(“结果”);
System.out.println(格式(“用%d除以%d得到%d”,分子、除数、结果));
}
}
}
}
包com.test.bdd.runner;
@RunWith(cumber.class)
@黄瓜选项(
glue=“com.test.bdd.steps”,
features=“classpath:cumber/calculator.feature”)
公共类RunCalculatorTests
{
}
所以我的桌子好像没法吃到小黄瓜。我想我的地图也有问题,但当我通过算术问题时,我会处理这些问题。

我需要做些什么来解决此算术不匹配问题?

功能文件中没有用于此场景步骤的数据表。不需要在此步骤定义中添加DataTable参数

这个例子似乎强调了ScenarioOutline的使用,而不是DataTable的使用

ScenarioOutline定义了一组步骤,迄今为止与场景类似。现在,可以使用examples表中的数据重复执行这些步骤。此表的每一行对应于所述步骤的一次执行。因此,如果你有3行,将有3次运行


另一方面,DataTable用于将数据作为一种列表发送到场景的特定步骤。比如购物清单。这只运行一次。虽然它也可以在场景大纲中使用,因此允许您使用变量列表,但功能文件中没有用于场景此步骤的数据表。不需要在此步骤定义中添加DataTable参数

这个例子似乎强调了ScenarioOutline的使用,而不是DataTable的使用

ScenarioOutline定义了一组步骤,迄今为止与场景类似。现在,可以使用examples表中的数据重复执行这些步骤。此表的每一行对应于所述步骤的一次执行。因此,如果你有3行,将有3次运行


另一方面,DataTable用于将数据作为一种列表发送到场景的特定步骤。比如购物清单。这只运行一次。虽然它也可以在场景大纲中使用,因此允许您使用变量列表

ArityMismatch意味着您没有传递正确数量的参数。 例如:

@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
“^I除以$”时的
@
公共无效iDivideNumeratorByDivisor()
{
该步骤使用2个参数(分子和除数),但该方法未指定这些参数

改用类似的方式: @当(^I除以$)时 公共无效iDivideNumeratorByDivisor(int-num,int-div)
{

ArityMismatch表示传递的参数数量不正确。 例如:

@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
“^I除以$”时的
@
公共无效iDivideNumeratorByDivisor()
{
该步骤使用2个参数(分子和除数),但该方法未指定这些参数

改用类似的方式: @当(^I除以$)时 公共无效iDivideNumeratorByDivisor(int-num,int-div)
{

Grasshopper,我注意到我没有一个正则表达式来捕获我的step文件中的值。我应该如何修改它来修复这个问题?我不确定我是否理解。你能添加更多细节吗Grasshopper,我注意到我没有一个正则表达式来捕获我的step文件中的值。我应该如何修改它来修复这个问题?我不确定我是否理解。你能添加更多的d吗蜗牛