Automating Integration Testing of Spring Boot Applications on Travis CI and Sauce Labs

by Paul Vorbach, 2016-12-01

Writing Unit Tests often is not enough to make sure your software is working correctly. In order to make sure your components are integrated correctly, writing automated Integration Tests will help you accomplish that goal. Unfortunately, setting up a project for automated integration testing is not straight forward. In this article I’ll show how to set up a Spring Boot web application with Maven that automatically runs on Travis CI and connects to Selenium WebDrivers running on Sauce Labs for testing with real browsers. This is done step-by-step so hopefully it will be easy for you to follow along and set up integration tests for your own Spring Boot project.

Prerequisites

You’re going to need accounts on GitHub, Travis CI and Sauce Labs to follow the instructions in this article. All of these services are entirely free for open source projects.

Local setup

In order to help with development and testing the setup, we’re first going to set up the environment locally. Let’s start with this simple pom.xml:

This is the main application class (src/main/java/org/example/webapp/WebApp.java):

And there’s also an HTML file we want to navigate our WebDriver to (src/main/resources/static/index.html):

Simple integration test

In order to be able to inject a Selenium WebDriver into the test classes, we add a Spring configuration class (src/test/java/org/example/webapp/integration/ITConfig.java):

Here’s a very basic integration test (src/test/java/org/example/webapp/integration/IndexTest.java):

That’s everything you need for the integration test itself. In order to run it locally, you’ll have to download the Selenium standalone server (get selenium-server-standalone-2.53.1.jar from here) as well as the geckodriver for your platform. Then put both the geckodriver binary and the selenium jar in a directory anywhere on your computer and run selenium from a command prompt:

java -jar selenium-server-standalone-2.53.1.jar -port 4445

From a separate command prompt, you can now run

mvn spring-boot:run

to start the application and

mvn test

in a third command prompt. Hopefully, you’ll see your test start Firefox after a while an it navigating to http://localhost:8080/.

Automating the configuration

The next step on our way is to be able to start our web app and the integration tests in a single command. This can be achieved using the maven-failsafe-plugin. Alter the build definition in your pom.xml to the following:

Failsafe adds several integration-test phases to the Maven lifecycle. We added an execution to the spring-boot-maven-plugin that starts the app before integration tests are run. The build-helper-maven-plugin is used to reserve a free port, so it won’t have port collisions if port 8080 is already in use.

Failsafe automatically detects integration tests by their name. All classes under src/test/java that end with IT will be executed by Failsafe instead of Surefire. Therefore we must rename the class IndexTest to IndexIT. Also we need to dynamically set the port of the requested URL in the test. Here’s how the test looks after these changes:

Now you should be able to run the integration tests with the following command:

mvn verify

Testing continuously

Since programmers are lazy and soon won’t want to run these time consuming tests on their own machines, it’s time to configure a CI server to do it for you. Travis CI is a solution that integrates well into GitHub and also has support to run Selenium tests via Sauce Labs.

First you need to enable the GitHub project in your Travis profile. This should only be one click:

Travis project activation
Travis project activation

Then you need to set up your project on Sauce Labs. I won’t cover this here, but once your project is set up, you will see your access key in your account settings. This access key is needed to verify the Travis CI server that runs your tests is allowed to control a Sauce Labs WebDriver. This key shouldn’t become publicy visible, so it makes sense to encrypt it. This can be done by using the Travis CLI tool (installation instructions).

Encryption is done with the following command:

travis encrypt "SAUCE_ACCESS_KEY=your-access-key"

This returns a string that you need for the file .travis.yml:

Replace your-user-name with the name of your Sauce Labs account and your-encrypted-access-key with the encrypted access key string. Additionally, Sauce Labs user name and access key need to be set as environment variables in the settings of your project as SAUCE_USERNAME and SAUCE_ACCESS_KEY (unencrypted).

When this is done, our ITConfig needs to be modified slightly to provide the RemoteWebDriver with a URL that contains the information of the Sauce Labs account:

Push these changes to GitHub and you’re done! You can watch your tests run on Travis and even watch the browser in your Sauce Lab account.

It works!
It works!

The complete sources of this sample project are available on GitHub.

Happy testing!