Getting Started with Maven and Cucumber in Eclipse
1. Getting Started with Maven and Cucumber in Eclipse
The purpose of this guide is to get the Maven and Cucumber plugins installed into eclipse and
to show how to use them to get a project started. There is plenty of documentation on how to
use the software at the command line but the plugin documentation is sorely lacking. Learn
more about Maven here- https://maven.apache.org/ and Cucumber here-
https://github.com/cucumber/cucumber-jvm.
Lets get started by installing the plugins into Eclipse. To install Maven open Eclipse and go to
Help>Install New Software. You should see this
http://download.eclipse.org/technology/m2e/releases/ this is also the same url that should be
put into the “work with” text box. You may have to restart Eclipse for the changes to take
effect. If you are having trouble, class path variables may need to be set. See here-
https://maven.apache.org/plugins/maven-eclipse-plugin/usage.html
Cucumber is just as easy to install. Use the same instructions as above however use
http://cucumber.github.com/cucumber-eclipse/update-site in the “work with” box.
We will also need some jar files to put into our project. We need to create a folder anywhere on
your hard drive to store them. There are four jars that are needed to make Cucumber run and I
added a fifth for Junit. The version numbers are the latest at this time.
cucumber-core- http://mvnrepository.com/artifact/info.cukes/cucumber-core/1.2.4
cucumber-java- http://mvnrepository.com/artifact/info.cukes/cucumber-java/1.2.4
cucumber-jvm-deps- http://mvnrepository.com/artifact/info.cukes/cucumber-jvm-deps/1.0.5
gherkin- http://mvnrepository.com/artifact/info.cukes/gherkin/2.12.2
cucumber-Junit- http://mvnrepository.com/artifact/info.cukes/cucumber-junit/1.2.4
Now we are ready to start a project. Still need Class path variables for
cucumber I think so?
2. Create a new Maven project. File>New>Other>Maven>Maven Project
4. Right click on your project and select New>File. Make sure it has the .feature extension.
Now we need to add the Cucumber jar files we downloaded earlier. Right Click on the project
and select Run As> Run Configurations. We are going to create a new Cucumber Feature with
the jar files we downloaded earlier. Select the Cucumber Feature on the left and create new
configuration. Give it a name and move to the Class path tab. Select your project and then click
on Add External Jars.
5. Go to where ever you saved those downloaded jar files select them all; then click open; then
apply; then run. You should see this:
No features found at [C:/workspace/CucumberProject/RangeTestCuke.feature]
0 Scenarios
0 Steps
0m0.000s
Nowwe have gottento the meatof the project.Cucumberhasitsown language calledGherkin.We will
write a reallysimple scenariowithoutgoingintodetailof whatthe language means.The detailscanbe
foundinthe bookcalled “The Cucumberfor JavaBook”. It usesthe commandline forexamplesbutthe
language isthe same.Get ithere- http://it-ebooks.info/book/4927/
6. In our simple projectwe wanttofindarange of numbersthat isbetween20and 30. Openupthe
RangeTestCuke.featurefilewe created.Putthisin.
Feature: Range
Scenario: Find the range between 20 and 30
Given the number is less than 20
When I find the range
Then number is out of bounds
NowRun As>Configurations>CukeTester.
Your outputshouldlooklike this:
You can implement missing steps with the snippets below:
@Given("^the number is less than (d+)$")
public void the_number_is_less_than(int arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^I find the range$")
public void i_find_the_range() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^number is out of bounds$")
public void number_is_out_of_bounds() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Great! Nowwe are readyto start writingsome code.
In yourprojectcreate a newclassinthe src/main/javafoldercalledRangeTest.
Copy the snippet into the new RangeTest.java
import cucumber.api.java.en.*;
import cucumber.api.PendingException;
public class RangeTest {
@Given("^the number is less than (d+)$")
public void the_number_is_less_than(int arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
7. @When("^I find the range$")
public void i_find_the_range() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^number is out of bounds$")
public void number_is_out_of_bounds() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
}
Thiswill give usnewoutput:
Feature: Range
Scenario: Find the range between 20 and 30 #
C:/workspace/CucumberProject/RangeTestCuke.feature:2
Given the number is less than 20 # RangeTest.the_number_is_less_than(int)
cucumber.api.PendingException: TODO: implement me
at RangeTest.the_number_is_less_than(RangeTest.java:10)
at ?.Given the number is less than
20(C:/workspace/CucumberProject/RangeTestCuke.feature:3)
When I find the range # RangeTest.i_find_the_range()
Then number is out of bounds # RangeTest.number_is_out_of_bounds()
1 Scenarios (1 pending)
3 Steps (2 skipped, 1 pending)
0m0.184s
cucumber.api.PendingException: TODO: implement me
at RangeTest.the_number_is_less_than(RangeTest.java:10)
at ?.Given the number is less than
20(C:/workspace/CucumberProject/RangeTestCuke.feature:3)
Lets write some code for the first step:
@Given("^the number is less than (d+)$")
public void the_number_is_less_than(int number) throws Throwable {
int rangeNumber = number; // Write code here that turns the phrase above
into concrete actions
// throw new PendingException();
}
8. Run the cuke testagain:
1 Scenarios (1 pending)
3 Steps (1 skipped, 1 pending, 1 passed)
0m0.087s
The firststephas nowpassed!Twomore to go.
@When("^I find the range$")
public void i_find_the_range() throws Throwable {
int number = rangeNumber; // Write code here that turns the phrase above into
concrete actions
// throw new PendingException();
}
@Then("^number is out of bounds$")
public void number_is_out_of_bounds() throws Throwable {
if (rangeNumber<20) // Write code here that turns the phrase above into
concrete actions
System.out.println("Number is out of bounds");
// throw new PendingException();
}
Feature: Range
Number is out of bounds
Scenario: Find the range between 20 and 30 #
C:/workspace/CucumberProject/RangeTestCuke.feature:2
Given the number is less than 20 # RangeTest.the_number_is_less_than(int)
When I find the range # RangeTest.i_find_the_range()
Then number is out of bounds # RangeTest.number_is_out_of_bounds()
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.073s
It all passed.Keepinmindthisisa very simple testusedonlyforanexample.Here isthe full javacode-
import cucumber.api.java.en.*;
import cucumber.api.PendingException;
public class RangeTest {
9. int number = 7;
int rangeNumber=0;
@Given("^the number is less than (d+)$")
public void the_number_is_less_than(int number) throws Throwable {
int rangeNumber = number; // Write code here that turns the phrase above
into concrete actions
return;
// throw new PendingException();
}
@When("^I find the range$")
public void i_find_the_range() throws Throwable {
int number = rangeNumber; // Write code here that turns the phrase above into
concrete actions
// throw new PendingException();
}
@Then("^number is out of bounds$")
public void number_is_out_of_bounds() throws Throwable {
if (rangeNumber<20) // Write code here that turns the phrase above into
concrete actions
System.out.println("Number is out of bounds");
// throw new PendingException();
}
}
Sources
https://maven.apache.org/
The Cucumberfor JavaBook by SebRose,Matt Wynne,andAslakHellesoy
http://it-ebooks.info/book/4927/
https://books.sonatype.com/m2eclipse-book/reference/introduction-sect-m2eclipse.html
http://www.tutorialspoint.com/maven/maven_eclispe_ide.htm
Tom Arend