Cucumber Java Runner Class Example

Migrating to Jira Cloud? Join Dec 1st webinar for strategies & tips. Learn more

When the feature story is ready, you can run your test to get code snippets for step definitions.

1. Create Test Runner

To run your BDD test, you must use one of the available runners. In this tutorial, we will use JUnit, but you can use any other.

  1. Create a new JUnit class:

    Created JUnit class

    Click the image to enlarge it.

  2. Add the following annotations to the class:

    Java

    package com.example.cucumber.test;

    import org.junit.runner.RunWith;
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;

    @RunWith(Cucumber.class)
    @CucumberOptions(
        format = {"pretty", "html:target/Destination"},
        features={"src/test/resources/UndoFeature.feature"}
        )
    public class UndoFeatureTest {
    }

    • The RunWith annotation commands to run the specified class instead of the created JUnit class.

    • The CucumberOptions annotation specifies different options for the Cucumber test. In our case, we specify the following options:

      • format – specifies the output format for the test result.

      • features – specifies the path to the feature file to run.

        Tip: You can specify the path to a folder. In this case, Cucumber will run all the feature files stored in that folder.

2. Run Test

In IntelliJ IDEA, you can run JUnit tests in different ways: from the Run menu, from the Project panel, from the context menu of the code editor. In this tutorial, we will show you how to run tests as part of a Maven build.

  1. Open the Maven Projects panel.

    Note: If the Maven Project panel is hidden, select View > Tool Window > Maven Projects from the main menu.
  2. Select the test phase from the Lifecycle list.

  3. Click Run on the panel's toolbar:

    Run the Maven build

    Click the image to enlarge it.

The Run panel shows the test log:

The test log

Click the image to enlarge it.

3. Get Method Stubs

Cucumber skips undefined test steps and generates code snippets for the missing test steps. You can find the generated snippets in the test log.

Example

Example Example

Running com.example.cucumber.test.UndoFeatureTest
Feature: Run the "Undo" command using hotkeys
  In order to check the undo functionality
  As a user
  I want Notepad to run the "Undo" command using the hotkeys Ctrl + z

  Scenario: Run the "Undo" command using hotkeys # src/test/resources/UndoFeature.feature:6
    Given I open Notepad                         # null
    And there is no text in Notepad              # null
    When I type "Some text"                      # null
    And I press [Ctrl + z]                       # null
    Then I should see no text in Notepad         # null

1 Scenarios (1 undefined)
5 Steps (5 undefined)
0m0.021s

You can implement missing steps with the snippets below:

@Given("^I open Notepad$")
public void i_open_Notepad() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();

}

@Given("^there is no text in Notepad$")
public void there_is_no_text_in_Notepad() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I type \"([^\"]*)\"$")
public void i_type(String arg1) throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I press \\[Ctrl \\+ z\\]$")
public void i_press_Ctrl_z() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I should see no text in Notepad$")
public void i_should_see_no_text_in_Notepad() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

Tests run: 5, Failures: 0, Errors: 0, Skipped: 5, Time elapsed: 0.225 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 5

  1. Create a new class in the same package as the test runner.

  2. Open the test log and copy the generated code snippets.

  3. Paste the code snippets to the created class. Now, you should have a class with a number of method stubs:

    Each method corresponds to a test step and consists of the following elements:

    • Annotation - The @Given, @When or @Then annotation that specifies the block of the scenario the test step belongs to.

    • Annotation parameter - A regular expression that represents the text of the test step after a keyword.

    • Java method - The method to execute.

  4. Currently, there are errors in the class. To fix them, import the following classes:

    Java

    import cucumber.api.PendingException;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;

Now, you should have a test class with a bunch of method stubs that do not perform the needed actions. Let's create test steps.

PrevNext

See Also

Behavior-Driven Development
About Behavior-Driven Development

palmerilaysence.blogspot.com

Source: https://support.smartbear.com/testleft/docs/bdd/tutorial/cucumber/get-stubs.html

Related Posts

0 Response to "Cucumber Java Runner Class Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel