The methods needed to map steps to actual Java code may be implemented in many step definition files. All step methods are global and visible to Cucumber. This means that the functionality needed may be implemented in more than one class. Global functions may feel horrible but there is an cunning idea behind having them global. If you describe your system using the exact same words and mean two different areas of your system, you have a larger problem than global methods. You will need to address this so parts that does different things in your system actually is described differently.
sample step definition and global methods are,
sample step definition and global methods are,
package com.shasta.bdd.steps; |
import com.shasta.bdd.calculator.Calculator; |
import cucumber.api.java.Before; |
import cucumber.api.java.en.Given; |
import cucumber.api.java.en.Then; |
import cucumber.api.java.en.When; |
import static org.junit.Assert.assertEquals; |
import static org.junit.Assert.assertNotNull; |
public class CalculatorSteps { |
private Calculator calculator; |
@Before |
public void setUp() { |
calculator = new Calculator(); |
} |
@Given ( "^I have a calculator$" ) |
public void i_have_a_calculator() throws Throwable { |
assertNotNull(calculator); |
} |
@When ( "^I add (\\d+) and (\\d+)$" ) |
public void i_add_and( int arg1, int arg2) throws Throwable { |
calculator.add(arg1, arg2); |
} |
@Then ( "^the result should be (\\d+)$" ) |
public void the_result_should_be( int result) throws Throwable { |
assertEquals(result, calculator.getResult()); |
} |
|