The fastest way to start building and automating BDD Stories via JBehave tool is to have the entire Framework/Project structure created via Maven Archetypes using the jbehave-simple-archetype.
This generates a project structure with all the necessary config and guidelines which help in implementing the Story files right away, and saves us a lot of time in writing a lot of boiler-plate-code.
Just navigate to the dir [lets say, C:\BDD\] where you want to keep all your BDD projects, and then enter the below in the cmd prompt to download the project via mvn
mvn archetype:generate -DarchetypeGroupId=org.jbehave -DarchetypeArtifactId=jbehave-simple-archetype -DarchetypeVersion=4.6.3 -DarchetypeRepository=https://nexus.codehaus.org/content/repositories/releases/org/jbehave/jbehave-simple-archetype/
Project Specification -
- When asked to enter a groupID, enter like: com.company.department
- When asked to enter an atifactID, enter like: bdd-jbehave
- When you are asked to define value for version just press enter to select the default version as 1.0-SNAPSHOT
- Let the default package be same as groupId so press Enter and confirm the details by typing “y” and then enter as seen below.
- After successful completion you will see a folder named after your artifactId in your workspace which contains a pom.xml and also src folder.
- This would also contain a sample MyStories.java and MySteps.java files, with default config
We get the following major classes/files
- MyStories.java - contains all config to run JBehave stories
- MySteps.java - contains implementation of all steps
- my.story - contains all the scenarios for a story
- pom.xml with all the dependencies and build config
That's it, your maven project is created. You can use this as a template to add your own story, steps and other classes.
To open the project in IntelliJ -
Select the option to import a new project from the IDE, and then specify the pom.xml for the newly created jbehave project.
On the subsequent windows, select the option to automatically download the Source and Documentation. This will automatically download all the packages that you import in your classes, as and when they are used, and will prevent package not found errors.
Ensure that the JBehave plugin is installed in IntelliJ, before importing the project.
Ensure that you select the 'Sources' and 'Documentation' checkboxes while importing, so that all the dependencies get downloaded.
[Optional step] To open the project in Eclipse -
Now cd into the folder jbehave which contains the pom.xml and then execute the following command:
mvn eclipse:eclipse
This command will make your project an eclipse project which can be opened through eclipse by adding the relevant .classpath and .project files.
That's it, you are ready to do BDD, just behave!