The code under test in this example builds upon the account management application created in an earlier blog post. DefaultAccountManager.java
implements a method for retrieving account information by ID via HTTP(s) and crediting a monetary amount to the existing balance of that account. The test DefaultAccountManagerIntegrationTest.groovy
requires the account management service to be up and running to verify the integration point.
It’s considered good practice to separate the source code of different test types with the help of dedicated directories. Furthermore, different types of tests should be runnable individually by invoking corresponding tasks. For example sometimes you might want to just run unit tests, other times will want to run integration tests.
Listing 1 demonstrates how to create a dedicated source set and Test
task for the purpose of integration testing. The logic has been extracted into a script plugin named integration-test.gradle
. Keeping the setup for integration testing separate from the main build script improves maintainability and readability.
gradle/integration-test.gradle
sourceSets {
integrationTest {
groovy.srcDir file('src/integrationTest/groovy')
resources.srcDir file('src/integrationTest/resources')
compileClasspath += sourceSets.main.output + configurations.testRuntime
runtimeClasspath += output + compileClasspath
}
}
task integrationTest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
mustRunAfter test
}
check.dependsOn integrationTest
Listing 1. Creating an integration test source set and task
The build.gradle
file in turn applies the integration test script plugin as shown below.
apply from: 'gradle/integration-test.gradle'
Listing 2. Applying the integration test script plugin
With this configuration in place, any source code under src/integTest/groovy
can be compiled and executed by running the integrationTest
task from the command line.
In the course of the next sections, you will set up the Docker plugin to retrieve the Docker image bundling the account management application. Moreoever, you will add tasks for starting and stopping a Docker container as fixture for integration testing.