Skip to content

Instantly share code, notes, and snippets.

@ahjashish
Last active August 11, 2016 16:04
Show Gist options
  • Save ahjashish/26c5ef3b93805a96980bc61dad15e5bf to your computer and use it in GitHub Desktop.
Save ahjashish/26c5ef3b93805a96980bc61dad15e5bf to your computer and use it in GitHub Desktop.
Cucumber with Spring dependency injection
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.artoves</groupId>
<artifactId>cucumber-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--Cucumber-->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!--Unit Testing-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
package runner;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
/**
* Created by artoves on 18/06/16.
*/
@RunWith(Cucumber.class)
@CucumberOptions(
format = {"pretty","html:target/cucumber.html"},
//format = {"pretty","json:target/cucumber.json"},
features = {"src/test/java/features"},
glue = "StepDefs"
)
public class CucumberRunner {
}
package service;
/**
* Created by artoves on 11/08/16.
*/
public interface NavigationService {
public void navigateToPage(String pageName);
public void pressButon();
public void redirectToPage(String pageName);
}
package service;
/**
* Created by artoves on 11/08/16.
*/
public class PetsNavigationServiceImpl implements NavigationService {
public void navigateToPage(String pageName) {
System.out.println("I navigated to the " + pageName +" page");
}
public void pressButon() {
System.out.println("I pressed the submit button");
}
public void redirectToPage(String pageName) {
System.out.println("I should be redirected to the " + pageName + " page");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean id="petsNavigationService" class="service.PetsNavigationServiceImpl"></bean>
<context:annotation-config/>
<context:component-scan base-package="service"/>
</beans>
Feature: Registration on the Website
Scenario: Register new pets
Given I am on the "New Pet" page
When I press "Register"
Then I should go to the "Register" page
package StepDefs;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import service.NavigationService;
/**
* Created by artoves on 18/06/16.
*/
@ContextConfiguration("classpath:cucumber.xml")
public class MyStepdefs {
private NavigationService petsNavigationService;
@Autowired
public MyStepdefs(NavigationService petsNavigationService) {
this.petsNavigationService = petsNavigationService;
}
public MyStepdefs() {
}
@When("^I press \"([^\"]*)\"$")
public void i_press(String arg1) throws Throwable {
petsNavigationService.pressButon();
}
@Then("^I should go to the \"([^\"]*)\" page$")
public void i_should_go_to_the_page(String pageName) throws Throwable {
petsNavigationService.redirectToPage(pageName);
Assert.assertTrue(pageName.equals("Register"));
}
@Given("^I am on the \"([^\"]*)\" page$")
public void iAmOnThePage(String pageName) throws Throwable {
petsNavigationService.navigateToPage(pageName);
Assert.assertTrue(pageName.equals("New Pet"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment