Spring MVC application with Cucumber and Selenium part 2

Submitted by Barnaby Golden on Wed, 19/06/2013 - 14:46

With the basic application in place it is now time to focus on testing. First up we add a Cucumber acceptance test. It is a good idea to keep your acceptance tests separate from your unit tests, so I tend to run them in the integration-test phase of Maven. To do this use the failsafe plugin, adding something like this to your pom:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.15</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

This will run test files ending in 'IT' by default and do so in the Maven integration-test phase. Next we need to add in the cucumber-jvm libraries to Maven:

  <dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>1.1.3</version>
  </dependency>
  <dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>1.1.3</version>
  </dependency>

And then we can set about creating some feature definition files. These files will reside in a package folder off of: src/test/resources Here is a very simple feature file:

Feature: Calculator
  Need to sums on numbers  
   
 Scenario: Add two numbers
   Given the number 10  
   When you add 7  
   Then the total is 17  

We hook up Cucumber to Java by adding an acceptance test to the src/test/java folder. As mentioned it should have a file name something like MathsIT.java so that it gets picked up by the failsafe plugin. The file needs to look something like this:

/**
 * 
 */
package com.blizzardtec.maths;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)  

/**
 * @author Barnaby Golden
 *
 */
public final class MathsIT {

}

The key bit here is the @RunWith line. This goes looking for the feature files on the classpath. At this stage the acceptance test should run but will recognise that it has no steps defined. So you need to add a steps file (once again the annotation identifies this to Cucumber). A dummy steps file is shown below:

/**
 * 
 */
package com.blizzardtec.maths;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import static org.junit.Assert.*;

/**
 * @author Barnaby Golden
 *
 */
public final class MathsSteps {

    private int val;

    @Given("^the number (\\d+)$")
    public void the_number(int arg1) throws Throwable {

        this.val = arg1;
    }

    @When("^you add (\\d+)$")
    public void you_add(int arg1) throws Throwable {

        this.val = this.val + arg1;
    }

    @Then("^the total is (\\d+)$")
    public void the_total_is(int arg1) throws Throwable {

        assertEquals("The result was not equal to the sum of the values", this.val, arg1);
    }
}

With the acceptance test added it is time to add a a simple unit test. Ensure that the JUnit library is on the path. In Maven this can be done by adding:

  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  </dependency>

You will also want to add the JUnit library in Eclipse. Then create the unit test (easily done in Eclipse) and put it in the appropriate package in the src/test/java directory. JUnit 4 lets you use annotations and you can do something like this:

/**
 * 
 */
package com.blizzardtec.maths;

import org.junit.Test;

/**
 * @author Barnaby Golden
 *
 */
public final class MathsTest {

    /**
     * Test add.
     */
    @Test
    public void testAdd() {
        //fail("Not yet implemented");
    }

    /**
     * Test multiply.
     */
    @Test
    public void testMultiply() {
        //fail("Not yet implemented");
    }

}

Source code for this Spring MVC Maven application can be found in this Github project.

In the final part of this post we add in Selenium.


 

Target Audience

© Copyright 2018 bhgagile Limited

Paperblog

Software Development Blogs - BlogCatalog Blog Directory