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.
-
Create a new class that extends the
JUnitStories
class: -
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");
}
} -
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));
}
...
} -
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:
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.
-
Open the Maven Projects panel.
Note: If the Maven Project panel is hidden, select View > Tool Window > Maven Projects from the main menu. -
Select the test phase from the Lifecycle list.
-
Click Run on the panel’s toolbar:
The Run panel shows the test log:
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:
-
Create a new class in the same package as the test runner.
-
Open the test log and copy the generated code snippets.
-
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.
-
-
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.
See Also
Behavior-Driven Development
About Behavior-Driven Development