Skip to content

Building a Test Automation with Spring Boot BDD

vvolverine edited this page Oct 24, 2018 · 13 revisions

This guide provides a sampling of how Spring Boot helps you accelerate and facilitate test automation development. As you read more Spring Getting Started guides, you will see more use cases for Spring Boot. It is meant to give you a quick taste of Spring Boot. If you want to create your own Spring Boot-based project, visit Spring Initializer, fill in your project details, pick your options, and you can download either a Maven build file, or a bundled up project as a zip file. (please refer guide for Spring Boot app here)

What you’ll build

You’ll build a simple test automation application with Spring Boot and add some useful services to it.

What you’ll need

  • About 15 minutes
  • A favorite text editor or IDE
  • JDK 1.8 or later
  • Gradle 4+ or Maven 3.2+

You can also import the code straight into your IDE:

  • IntelliJ IDEA

How to complete this guide

Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.

  • To start from scratch, move on to Build with Maven.
  • To pick complete version please move here

Build with Maven

First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Maven is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.

Create the directory structure In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello on *nix systems.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>sprimber-parent</artifactId>
    <groupId>com.griddynamics.qa</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.griddynamics.qa</groupId>
<artifactId>sprimber-test</artifactId>

<dependencies>
    <dependency>
        <groupId>com.griddynamics.qa</groupId>
        <artifactId>sprimber-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.2.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-maven</artifactId>
            <version>2.8</version>
            <configuration>
                <resultsDirectory>${project.parent.basedir}/allure-results</resultsDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

The Spring Boot Maven plugin provides many convenient features:

  • It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
  • It searches for the public static void main() method to flag as a runnable class.
  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.

Create a simple test automation application

Now you can create a class with step definitions for a simple test automation application.

test/steps/BaseEchoSteps.java

@Actions
public class BaseEchoSteps {

private static final Logger LOGGER = LoggerFactory.getLogger(BaseEchoSteps.class);

public BaseEchoSteps() {
}

@Before
public void before() {
    LOGGER.info("Hey, I'm before action");
}

@BeforeStep
public void beforeStep() {
    LOGGER.info("Hey, I'm beforeStep action");
}

@Given("^test given action$")
public void given() {
    LOGGER.info("Hey, I'm given action");
}

@When("^test when action$")
public void when() {
    LOGGER.info("Hey, I'm when action");
}

@When("some when action with param '{word}'")
public void whenWithParameters(String word) {
    LOGGER.info("Hey, I'm when action with param {}", word);
}

@Then("test then action")
public void then() {
    LOGGER.info("Hey, I'm then action");
}

@Then("every time failed action")
public void thenFail() {
    LOGGER.info("Hey, I'm every time failed action");
    Assertions.assertThat("true").as("True should never be false").isEqualTo("false");
}

@AfterStep
public void afterStep() {
    LOGGER.info("Hey, I'm afterStep action");
}

@After
public void after() {
    LOGGER.info("Hey, I'm after action");
}

public void dummy() {
    LOGGER.info("Hey, I'm not an action");
}
}
Clone this wiki locally