首页 > 解决方案 > Arity 与 Java Cucumber 数据表不匹配

问题描述

我一直在使用Jakub Czeczótka 的精彩博客学习 Cucumber for Java 。但他在描述数据表时迷失了我。
我做了一个带有加法、减法、乘法和除法的计算器类。每种方法的单独测试都通过了。当我尝试使用表时,我从 Gherkin 收到一个 arity mismatch 错误:

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 问题时会处理这个问题。
我需要做些什么来解决这种数量不匹配的问题?

标签: javacucumber

解决方案


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

@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{

该步骤使用 2 个参数(分子和除数),但该方法未指定这些参数。

改用这样的东西:@When("^I 除以 $") public void iDivideNumeratorByDivisor(int num, int div) {


推荐阅读