Gradle and Testing

Guilherme Gouveia
CIn Open Source
Published in
4 min readJun 3, 2020

Gradle is an open-source build automation tool, which in other words means that it helps building your project. The word build can have some mixed scope definitions, but with gradle it goes with the wider one. It can help you with project management, dependencies, exporting a .zip, and much more. It’s versality has made it gain a lot of popularity in the recent years. This tutorial will get you going on starting using this tool and enable tests in your project using it.

This tutorial will be written with a UNIX based system in mind, with command line instructions (CLI) and assuming you are at root of your project(where gradle is), so ./CLI. If otherwise, you have to put the path to the gradle root.

To start things out, you have to install the framework. This can be done easily on whathever IDE you use, or on the gradle website itself.

Later on, you have to actually start a gradle project. Like project-wide tools, there is a command line to do just that. Type gradle init in the root folder of your project to create the base files to start your project. These are:

build.gradle : main user configurable file to define gradle main functions in your project.

settings.gradle : more technical file also with settings to configure gradle.

This will enable some CLI you can do at your project. You can always type gradle help to play around, but most important, gradle build will build your project. To see a list of possible Tasks, type gradle tasks. We will see a little more about these later on, but it’s good to begin seeing these lists.

With this done, you can begin tweaking with this incredible tool.

The main thing you would like with Gradle it’s to add a dependency to your project. This will make so any external code you need will be automatically downloaded when people clone your project without much trouble. This is essential nowadays since you use so much code that is already build, and is made such an easy task with Gradle.

It’s very similar to Maven as it main resource for dependencies is the Maven Central. Simply put your dependency in your build.gradle between the “dependencies” curly braces as so:

dependencies {

implementation ‘junit:junit:4.1’, ‘org.hamcrest:hamcrest-all:1.3’

}

This would add junit to your project, enabling you to call junit classes and functions in your code. There is a hierarchy system so any dependencies junit need would also be added.

The “implementation” keyword can be changed a little depending on what you want. More specifically, I would want Junit only on my tests classes, so I could change to “testImplementation”.
If you want to see all dependecy tree, type gradle dependencies.

To start with automated testing you will need, of course, a class to test. We will be doing this in Java, and the class will be a simple Binary Search one:

public class BinarySearch {
public static int binarySearch(int[] a, int key) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int midVal = a[mid];
if (midVal < key) low = mid + 1;
else if (midVal > key) high = mid - 1;
else return mid; // key found
}
return -(low + 1); // key not found.
}
}

You will need a test file and classes, if everything done right you should have a test source, in the same project level as your main source. Mimic the folder tree as in your main source, and create the test class:

import org.junit.Test;
import org.junit.Assert;
import org.junit.Before;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.util.Arrays;

public class BinarySearchTest {

int[] a;

@Before
public void setup() {
a = new int[]{7,9,1,3,5,13,20,55,255,127};
Arrays.sort(a);
}

@Test
public void elementFound() {
// junit style
Assert.assertEquals(2, BinarySearch.binarySearch(a, 5));
}

@Test
public void elementNotFound() {
assertThat(BinarySearch.binarySearch(a, 15), is(lessThan(0)));
}

@Test
public void emptyArray() {
assertThat(BinarySearch.binarySearch(new int[]{}, 99), is(lessThan(0)));
}

@Test(expected=NullPointerException.class)
public void nullArray() {
BinarySearch.binarySearch(null, 99);
}

}

This is common Junit syntax. It’s basically test to check some simple things given an input array, which in this case is {7,9,1,3,5,13,20,55,255,127}.

You are pretty much done. Gradle automatically creates a test Task for you that you can run with gradle test. You can see that task and all other with the command gradle tasks as previously explained.

Following the tutorial this is probably your first touch with these things called tasks. These are a major Gradle component, and can be used to automate more user-defined things. For example, if you want to do another tests source, say, a integration tests one, you can define a task to run gradle integrationTest, for example.

Running the already defined gradle test will output you the tests results, if any(there could be runtime errors aswell). This type of automated build that Gradle uses is extremely useful because you can merge with any other testing framework beside junit, say, Jgiven and code the tests in any way you want, and still be automated and easy to run.

This is the basics to start developing automated tests with Gradle. Note that this is only a scratch of what this tool can do, but with this, you can start tweaking better with the dependencies and have a bulk project testable easily.

--

--