What is "cucumber.runtime.CucumberException: Arity mismatch: Step Definition" in java testing?
Asked Answered
D

1

0

I have written a code to test adding items into an inventory but i keep getting Arity mismatch error.

My code is as follows:

@When("^(Coffee|Milk|Sugar|Chocolate) is (.*)$")
    @Then("^Inventory is successfully added\\.$")
    public void inventory_is_successfully_added(String Coffee, String milk, String sugar, String chocolate) throws InventoryException {
        
        coffeeMaker.addInventory(Coffee,milk,sugar,chocolate);
        System.out.println("Inventory is successfully added.");
    }

and the corresponding Scenario written in feature file is this:

Scenario: Add Inventory
        When Coffee is 4
        When Milk is 7 
        When Sugar is 0
        When Chocolate is 9
        Then Inventory is successfully added.

This is the error I get:

cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'edu.ncsu.csc326.coffeemaker.TestSteps.inventory_is_successfully_added(String,String,String,String) in file:/D:/newEclipse/Assign/build/classes/java/test/' with pattern [^(Coffee|Milk|Sugar|Chocolate) is (.*)$] is declared with 4 parameters. However, the gherkin step has 2 arguments [Coffee, 4].

can someone please tell me what it means? TIA

Dang answered 23/8, 2021 at 9:42 Comment(0)
P
1

This Exception means that the number of capture groups in your step does not match the number of parameters defined in your step definition.

In your example, the step @When("^(Coffee|Milk|Sugar|Chocolate) is (.*)$") has 2 capture groups: (Coffee|Milk|Sugar|Chocolate) and (.*) and the step @Then("^Inventory is successfully added\\.$") has no capture groups. The step definition public void inventory_is_successfully_added(String Coffee, String milk, String sugar, String chocolate) defines 4 parameters: String Coffee, String milk, String sugar, String chocolate.

Cucumber is throwing this exception, because it doesn't know which values to pass to the parameters.

To fix this:

  1. Implement the step where you add the amount of (Coffee|Milk|Sugar|Chocolate) if you haven't already (your example does not provide the implementation).
  2. Implement the step definition for inventory_is_successfully_added() so that the method does not take any parameters, and inside the method get the values provided for coffee, milk, etc from your test context.
Polymorphism answered 20/10, 2021 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.