Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Cucumber: hello World application in Eclipse

In my previous post, I explained how to setup cucumber in command line mode, in this post I am going to show you how to execute a cucumber application in eclipse.

Below step-by-step procedure explain complete working example.

Step 1: Open Eclipse. Create new maven project ‘cucumber_tutorial’.

Right click on project explorer -> New -> Other


Select ‘Maven Project’ and press Next.

Select the check box 'Create a simple project (skip archetype selection)'.

Press Next.

Update Group Id and Artifact Id as ‘cucumber_tutorial’.

Press Finish button.

Total project structure looks like below.

Step 2: Update pom.xml for maven dependencies. I am using below maven dependencies.
 


io.cucumber
cucumber-core
2.3.1




io.cucumber
cucumber-java
2.3.1




io.cucumber
cucumber-jvm-deps
1.0.6
provided




io.cucumber
gherkin
5.0.0




junit
junit
4.12
test




io.cucumber
cucumber-junit
2.3.1
test





I faced some proxy problem while downloading these libraries. I downloaded them manually and added to my project explicitly.

Step 3: Create new class ‘LoginUtil.java’ under the package ‘com.sample.util’.

LoginUtil.java
package com.sample.util;

public class LoginUtil {

/**
*
* @param userName
* @param password
* @return true if the login successful, else false
*/
public static boolean isLoginSuccessful(String userName, String password) {

if (userName == null || userName.isEmpty() || password == null || password.isEmpty()) {
return false;
}

if ("krishna".equals(userName) && "password123".equals(password)) {
return true;
}

return false;
}
}

Create a package ‘com.sample.features’ and define the file Login.feature.

Login.feature
Feature: Login

Login should be quick and friendly.

Scenario: Successful Login
Users should be logged in successfully by providing correct username and password.

Given I have chosen to Login
When I log in with user name "krishna" and password "password123"
Then I should be logged in successfully
And I should see a personalized greeting message

Create a class ‘LoginTest.java’ in the package ‘com.sample.test’.

LoginTest.java
package com.sample.test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.sample.util.LoginUtil;

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

public class LoginTest {
private String userName;

@Given("^I have chosen to Login$")
public void iHaveChosenToLogin() throws Exception {
System.out.println("Opening login page");
}

@When("^I log in with user name \"([^\"]*)\" and password \"([^\"]*)\"$")
public void iLogInWithCorrectAnd(String userName, String password) throws Exception {

if (LoginUtil.isLoginSuccessful(userName, password)) {
this.userName = userName;
System.out.println("Login successful");
return;
}
assertFalse("Wrong Credentials. userName " + userName, true);
}

@Then("^I should be logged in successfully$")
public void iShouldBeLoggedInSuccessfully() throws Exception {
System.out.println("Logged in Successfully");
}

@Then("^I should see a personalized greeting message$")
public void iShouldSeeAPersonalizedGreetingMessage() throws Exception {
System.out.println("Welcome " + userName);
assertTrue(true);
}
}

Define the package 'com.sample.main' and define class TestRun.java.

TestRun.java
package com.sample.main;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, features = "src/test/java/com/sample/features", glue = "com.sample.test", plugin = {
"pretty" })
public class TestRun {

}


Run the application as junit test, you can able to see below messages in console.
Feature: Login
Login should be quick and friendly.

Scenario: Successful Login # src/test/java/com/sample/features/Login.feature:5
Users should be logged in successfully by providing correct username and password.
Opening login page
Given I have chosen to Login # LoginTest.iHaveChosenToLogin()
Login successful
When I log in with user name "krishna" and password "password123" # LoginTest.iLogInWithCorrectAnd(String,String)
Logged in Successfully
Then I should be logged in successfully # LoginTest.iShouldBeLoggedInSuccessfully()
Welcome krishna
And I should see a personalized greeting message # LoginTest.iShouldSeeAPersonalizedGreetingMessage()

1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.262s



Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Cucumber: hello World application in Eclipse

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×