java - 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 问题时会处理这个问题。
我需要做些什么来解决这种数量不匹配的问题?
解决方案
ArityMismatch 意味着您没有传递正确数量的参数。例如:
@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
该步骤使用 2 个参数(分子和除数),但该方法未指定这些参数。
改用这样的东西:@When("^I 除以 $") public void iDivideNumeratorByDivisor(int num, int div) {
推荐阅读
- python - 有没有办法在 TensorFlow 会话中调用 Numpy 函数?
- php - 如何使用 GET 方法使用 php 创建电子商务过滤器
- css - 基本 url em slded css data-lazyload
- django - UserCreationForm 中的字段多于预期
- javascript - 向 npm 脚本提供部分路径名(作为参数)
- python-3.x - 跳过空列表并继续执行功能
- reactjs - 如何避免在 redux thunk 中使用单独的 _PENDING _FULFILLED 和 _REJECTED 操作?
- node.js - 当我添加 $inc 运算符以更新节点、mongodb 应用程序中的数据时,日期不更新
- python - 不同的整数组
- c# - 如何防止 JSON 解析错误:我的 RootObject 类将属性定义为“long”,但有时我得到一个“object”