3. Create Test Runner and Get Code Stubs

Applies to TestLeft 15.40, last modified on March 17, 2022

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

1. Create Test Runner

You can run JBehave tests in various ways. In this tutorial, we will create a new class that extends the JUnitStories class. It allows running JBehave tests as normal JUnit tests.

  1. Create a new class that extends the JUnitStories class:

    The created test class

    Click the image to enlarge it.

  2. This class must implement the storyPaths method that returns the paths to the feature files. Add the following method to the class:

    Java

    import org.jbehave.core.junit.JUnitStories;

    import java.util.Arrays;
    import java.util.List;

    public class UndoTypingTest extends JUnitStories {
        
        protected List<String> storyPaths() {
            return Arrays.asList("UndoFeature.story");
        }
    }

  3. To command JBehave to generate code snippets for the missing test steps, add the configuration method to the class:

    Java

    import org.jbehave.core.configuration.Configuration;
    import org.jbehave.core.configuration.MostUsefulConfiguration;
    import org.jbehave.core.reporters.Format;
    import org.jbehave.core.reporters.StoryReporterBuilder;

    public class UndoTypingTest extends JUnitStories {

        @Override
        public Configuration configuration() {
            return new MostUsefulConfiguration()
                    .useStoryReporterBuilder(new StoryReporterBuilder()
                        .withFormats(Format.CONSOLE));
        }

        ...

    }

  4. Also, we need to "tell" the runner where it should look for step definitions:

    • Add a new class in the same package as the test runner. This class will contain step definitions.

    • Add the stepsFactory method:

      Java

      import org.jbehave.core.steps.InjectableStepsFactory;
      import org.jbehave.core.steps.InstanceStepsFactory;

      public class UndoTypingTest extends JUnitStories {

          @Override
          public InjectableStepsFactory stepsFactory() {
              return new InstanceStepsFactory(configuration(),
                      // Create an instance of the class with step definitions
                      new UndoTypingSteps());
      }

          ...

      }

Final Code

The final code of the runner class is as follows:

Example

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

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

Example

  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:

    Java

    public class UndoTypingSteps {
        @Given("I open Notepad")
        @Pending
        public void givenIOpenNotepad() {
            // PENDING
        }

        @Given("there is no text in Notepad")
        @Pending
        public void givenThereIsNoTextInNotepad() {
            // PENDING
        }

        @When("I type \"Some text\"")
        @Pending
        public void whenITypeSomeText() {
            // PENDING
        }

        @When("I press [Ctrl + z]")
        @Pending
        public void whenIPressCtrlZ() {
            // PENDING
        }

        @Then("I should see no text in Notepad")
        @Pending
        public void thenIShouldSeeNoTextInNotepad() {
            // PENDING
        }
    }

    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.

      Note: The @Pending annotation commands JBehave to skip the method, since it is not implemented.
    • 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 org.jbehave.core.annotations.Given;
    import org.jbehave.core.annotations.Pending;
    import org.jbehave.core.annotations.Then;
    import org.jbehave.core.annotations.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.

Prev     Next

See Also

Behavior-Driven Development
About Behavior-Driven Development

Highlight search results