Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titlesrc/test/java/calculator/StepDefinitions.java
collapsetrue
package calculator;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import com.xray.tutorials.Calculator;

import static org.junit.Assert.*;

public class StepDefinitions {
    private Integer int1;
    private Integer int2;
    private Integer result;


    @Given("I have entered {int} into the calculator")
    public void i_have_entered_into_the_calculator(Integer int1) {
        this.int2 = this.int1;
        this.int1 = int1;
    }
    
    
    @When("I press add")
    public void i_press_add() {
        this.result =  Calculator.Add(this.int1, this.int2);
    }

    @When("I press multiply")
    public void i_press_multiply() {
        this.result =  Calculator.Multiply(this.int1, this.int2);
    }


    @Then("the result should be {int} on the screen")
    public void the_result_should_be_on_the_screen(Integer int1value) {
        // Write code here that turns the phrase above into concrete actions
        assertEquals((Integer)(this.int1 + this.int2)assertEquals(value, this.result);
    }
    

}
  • the test runner is defined in the RunCucumberTest class. Cucumber options can be overriden from the command line, whenever executing Maven.

...

Code Block
languagejava
titlebuggy Multiply() method in Calculator.java
collapsetrue
// Multiply two integers and retuns the result... this code is buggy on purpose
public static int Multiply(int num1, int num2 )
{
    if ((num1==1) ||0) {
        return num2;
    } else if (num2==1)0) {
        return 0num1;
    } else {
        return num1 * num2;
    }
}

...

Code Block
languagejava
titlesrc/test/java/calculator/StepDefinitions.java
collapsetrue
package calculator;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import com.xray.tutorials.Calculator;

import static org.junit.Assert.*;

public class StepDefinitions {
    private Integer int1;
    private Integer int2;
    private Integer result;


    @Given("I have entered {int} into the calculator")
    public void i_have_entered_into_the_calculator(Integer int1) {
        this.int2 = this.int1;
        this.int1 = int1;
    }
    
    
    @When("I press add")
    public void i_press_add() {
        this.result =  Calculator.Add(this.int1, this.int2);
    }

    @When("I press multiply")
    public void i_press_multiply() {
        this.result =  Calculator.Multiply(this.int1, this.int2);
    }


    @Then("the result should be {int} on the screen")
    public void the_result_should_be_on_the_screen(Integer int1) {
        // Write code here that turns the phrase above into concrete actionsvalue) {
        assertEquals((Integer)(this.int1 + this.int2)value, this.result);
    }
    

}
  • the test runner is defined in the RunCucumberTest class. Cucumber options can be overriden from the command line, whenever executing Maven.

...

Code Block
languagejava
titlebuggy Multiply() method in Calculator.java
collapsetrue
// Multiply two integers and retuns the result... this code is buggy on purpose
public static int Multiply(int num1, int num2 )
{
    if ((num1==1) ||0) {
        return num2;
    } else if (num2==10)) {
        return 0num1;
    } else {
        return num1 * num2;
    }
}

...