Automated Java tests with Gradle and Travis CI

Matthew Glover
4 min readJul 2, 2017

--

The first challenge I have been set at 8th Light is to build a Tic Tac Toe game in Java using TDD. So far I have been running my tests via IntelliJ. This works for immediate feedback, but when sharing code it’s helpful to use a CI tool like Travis, which will automatically run test on commit to Github.

Once up and running, this kind of workflow is extremely rewarding and easy to use. Unfortunately there is a significant up-front cost to be paid in terms of configuring all the different pieces to work together, before you can reap the rewards. I therefore set myself the challenge to create the simplest Java project (consisting of just a couple of trivial tests), which I could then push to Github and, on push, would trigger Travis CI to run the full test suite. As an additional challenge, I also wanted to use IntelliJ to control the Git repo and pushing to Github.

Overview

I always find it helpful to have an overview of what we’re trying to achieve before getting too involved in the specifics. So this is the basic structure:

  • Code will be written in IntelliJ, and IntelliJ will manage the Local Git repo and synchronising the local repo with Github.
  • Gradle is a popular build tool for Java projects and, amongst other things, it can be used to run a Java test suite
  • Github is a place to host a remote copy of the Git repo and provides integrations with 3rd party services, including Travis CI
  • Travis CI is a web-based continuous integration service, which takes the Git repo, and using a simple configuration file, compiles the code in a docker container and then runs the test suite using Gradle
  • Travis CI provides a web console for viewing details of the build and will also report back to Github on the status of the test build

Step 1: Create a Gradle project in IntelliJ

Create a new project, on the first screen select Gradle project, Java version 1.8, and under Additional Libraries and Frameworks, select Java. Click Next

Step 2: Group and Artifact ID

Set Group Id to com.matthewglover and Artifact Id to TravisGradleExample (or whatever Group and Artifact Ids you want). Click Next

Step 3: Additional project options

Check Create directories for empty content routes automatically (which sets up empty directories for src and test code. Click Next and Finish (change the project location as required).

Step 4: Project overview

IntelliJ will generate a basic Gradle project structure. Of particular note:
src/main/java: for Java application code
src/test/java: for JUnit tests
build.gradle: the gradle build configuration file (we’re just going to use the default file, which IntelliJ by default, includes JUnit 4.

Step 5: Create a test file

Create a new Java Class in the testdirectory called SimpleTest and add a single test like so (it does nothing, but will pass):

Check the test runs locally via IntelliJ (which it should).

Step 6: Check Gradle test suite runs

Open the Gradle Tool Window: View > Tool Windows > Gradle. In the Gradle window, open Tasks > verification, then double click the test task. This should report that “All Tests Passed” and that the build was successful.

Step 7: Create Git Repo, commit existing files and upload to Github

  1. Select VCS > Enable Version Control, select Git
  2. Select VCS > Commit Changes, then check Unversioned Files and provide a Commit Message of “Initial commit”
  3. Select VCS > Import into Version Control > Share Project on Github, accept default options and click Share
  4. Browse to your Github repo and you should now have a new TravisGradleExample Repo (e.g. https://github.com/matthewglover/TravisGradleExample)

Step 8: Preparing for Travis CI

The Travis documentation explains that Travis will run the command gradle check. So we should first check we can run this from our own command line, which should report “Build Successful”. This means that, provided we configure Travis correctly, Travis should also report a successful build.

Travis is configured using a .travis.yml file, which we’ll add now at the project root:

This tells Travis we’re using Java 1.8. Travis will detect it’s a Gradle project and so know to run the gradle check command.

Step 9: Enabling Travis for the new Repo

Assuming you’re set up on Travis already, log into Travis, go to your accounts, resync repos and enable Travis with your new repo.

Step 10: Create a fork and push to Github

Select VCS > Git > Branches..., create a new branch, call it travis_integration making sure Checkout branch is selected.

Next commit the changes to the branch (i.e. our new .travis.yml file) by clicking the Commit Changes button. Add a commit message and the click Commit and push then, accepting the default, click Push.

Step 11: Create a pull request

Go to your Github repo and create a pull request for your newly pushed branch. Github should then warn that it is running a build on Travis. Click on the link and you should see your new project running on Travis. Click through on the link and you should eventually get a passing build.

And we’re done!

--

--