Sei sulla pagina 1di 6

Java Integration Test in Travis with PR Coverage

ArindamNayak

31 Aug 2019 CPOL

Integration test in Travis with coverage feedback for PRs

Download source - 69.1 KB

Introduction
This article explains how to setup the integration test with a hosted CI, here I am using travis. Along with the setup, I will explain
how you can publish the coverage report to pull request. This is also important as it will give feedback to the PR contributor in terms
of how coverage got impacted.

Disclaimer
This does not use any ready to use coverage service such as codecov or any other tool. This uses a plain script based way and gradle
dependencies (as this will free you from the paid services). This uses the spring project and junit 5 testcase for demonstration
purposes. Since it mostly deals with testcase and coverage, there is no controller or API as such. For coverage, it won't report the
delta change, rather it is overall coverage including the changed code. This can be done, but is currently not in the scope.

Also, this article mostly concentrates on how to hook up integration test in travis and coverage. I am planning to add an article
explaining the integration test.

Background
We usually write integration tests which need actual DB access and other services dependency. And we are comfortable in running
those in a local environment. It is important to run those in the build process, because if any test case fails at the CI step, it will be
reported for every build.

Pre-setup
Now, we will jump to the pre-setup part. I have taken a spring boot based project. It is a simple employee CRUD application. It has
data access layer and service layer. It contains certain validation such as the following:

Employee email should be in the correct format.


While updating employee, it should get the existing employee id.
While updating, it cannot use an existing email of another employee.
Non-existent employee id can't be deleted.

All those test cases are written using new JUnit 5. I have already explained about certain important features of jUnit 5 here. I have
written the integration test by covering the above cases. Here is the link to the source code.
Source code

Setup
Here, we will be using hosted CI as travis. Travis as subscription for both private and public repo. To get a travis account, you have to
sign up at https://travis-ci.org, for private repo, it will be https://travis-ci.com/. Sign in with your github credential, it will ask you for
repo permission which it needs to list the repository and access those to build the source code. Please go ahead with permission.

Then, enable the build process for your repository. Here is how I configured it.

Next, to initiate travis build, you need to have a YAML file named ".travi.yml" at the top level of your repo. Here is what I will be
using.

addons:
mariadb: '10.1'
language: java
dist: trusty
jdk:
- openjdk8
before_install:
- wget http://services.gradle.org/distributions/gradle-5.4-bin.zip
- unzip -qq gradle-5.4-bin.zip
- export GRADLE_HOME=$PWD/gradle-5.4
- export PATH=$GRADLE_HOME/bin:$PATH
- gradle -v
script:
- mysql --user="root" --execute="CREATE SCHEMA IF NOT EXISTS demo_test;"
- mysql -u root demo_test < src/main/resources/migration/V1.1__create_employee_table.sql
- mysql -u root demo_test < src/main/resources/migration/V1.2__seed_employees.sql
- gradle clean build test
- gradle check > build.out
after_success:
- chmod +x ./src/main/resources/util_script/pr_comment.sh
- ./src/main/resources/util_script/pr_comment.sh
before_cache:
- sudo chown -R travis:travis $HOME/.gradle/caches/modules-2
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
cache:
directories:
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
notifications:
email:
- arindam.nayak@outlook.com

I will explain each of lines mentioned above in the YAML file.

Since we are dealing with integration test, we need to have actual DB in CI. Travis provides lot of DB option during build
process. Here is the list of those. In our case, I chose "mariadb", and first 2 lines explain that.
Since this is a Java project, I used "language" = "java". The build machine is an ubuntu machine with trusty release.
Here, we are taking openjdk8 as jdk option.
Since JUnit 5 depends on gradle 5, I have made travis to use gradle 5.4. Line number 7 -12 explains that.
Line number 13 to 18 prepares the database inside CI for integration test. It first initializes the database, then applies the
migration needed for data. Here, we could use flyway process to do it. But currently, it is plain SQL scripts.
Line number 19, 20 publish the coverage to the corresponding PR.
Rest all are for caching the gradle dependency and notification.

The crucial part here is to setup the database and other services if any in the travis machine itself, so that the Java integration test
can find those. Here, we do that by adding the mariadb and applying the SQL scripts so that seed data and database is available
while running the tests.

Here, you can check the build logs by clicking on the following:

This is how build log looks like:

Coverage Setup
I have used jacoco and com.github.ksoichiro.console.reporter gradle. plugin to get the coverage and get
coverage as usable text respectively.

id 'jacoco'
id 'com.github.ksoichiro.console.reporter' version '0.6.2'
To post the coverage to the corresponding PR, you have to create one github user and get that user's token. We need this posting
to PR need a git user with posting access. Since all those are done automatically, we need the token to be used in API.

Create a github user and generate a token by going to Settings --> Personal Access Token --> Generate Token. You should give
write discussion and public repo permission for the token. Here is how it looks like:

Then, note down the token and configure that in travis. In travis settings (https://travis-ci.org/repo_slug/settings), set the
environment variable as shown below:

We have created a pr_comment.sh which actually posts the coverage to PR and that is run as part of travis process (refer to line
number 19 and 20 of .travis.yml). Here is that script:

#!/bin/sh

echo "build out"


cat build.out
echo "build out done"
if [ "$TRAVIS_PULL_REQUEST" != "false" ] ; then
curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST \
-d "{\"body\": \"Updated coverage: $(cat build.out | grep junit)\"}" \
"https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments"
fi

This script, makes a curl request for PR to github API. That request posts a comment to the PR about the coverage. In line number
18 of .travis.yml (gradle check > build.out), we saved the coverage information in build.out file. "Check" is the command which
"com.github.ksoichiro.console.reporter" gradle plugin provides and it parses the jacoco XML to get the
coverage information. Finally, after coverage posting, here is how it looks like in PR. I have created a PR in my repo for
demonstration purposes.
Points of Interest
There are a lot of things that can be done for coverage and integration test. I am listing those. You can add a lot more dependency
to the travis build process if your application depends on those. E.g., database, cache, solr, queueing service, etc. In terms of
coverage, gradle provides lot of options like mark the build as failure if certain % of coverage is not met and there can be a lot more
to it.

History
This is my initial published content. I will keep the list of revisions here.

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


ArindamNayak
Software Developer
India

Developing web based application since last 5 years, also interested in designing application with optimized used of available
tech stacks, as well as apply those and real life experience to help out friends, colleagues,communities such as this or other
forums.

MCSD (2013) certified developer.

Reach me - http://about.me/arindamnayak

Comments and Discussions


0 messages have been posted for this article Visit https://www.codeproject.com/Articles/5166089/Java-Integration-Test-in-
Travis-with-PR-Coverage to post and view comments on this article, or click here to get a print view with messages.

Permalink Article Copyright 2019 by ArindamNayak


Advertise Everything else Copyright © CodeProject, 1999-2019
Privacy
Cookies Web01 2.8.190901.1
Terms of Use

Potrebbero piacerti anche