After we have finished this lesson, we:
- Understand how we can compile unit tests which use the Groovy programming language.
- Know how we can run unit tests which use Spock Framework.
- Can run only unit tests which belong to a specific JUnit 4 category.
The Requirements of Our Gradle Build
The requirements of our Gradle build are:
- Our application is written by using the Java programming language.
- It must support unit tests which use Spock framework.
- The source directory of our unit tests must be: src/test/groovy.
- The resources directory of our unit tests must be: src/test/resources.
- It must run only unit tests which belong to the
UnitTestcategory.
Let’s move on and find out how we can get the required dependencies with Gradle.
Getting the Required Dependencies
We can get the required dependencies by declaring the following dependencies in our build.gradle file:
- JUnit (version 4.12) is a framework that allows us to write automated tests with Java programming language.
- Groovy All (version 2.4.11). Groovy is a dynamic programming language for the JVM.
- Spock Core (version 1.1-groovy-2.4). Spock is a testing and specification framework for Java and Groovy applications.
After we have added these dependencies to our build.gradle file, its dependencies block looks as follows:
dependencies {
testCompile(
'junit:junit:4.12',
'org.codehaus.groovy:groovy-all:2.4.11',
'org.spockframework:spock-core:1.1-groovy-2.4'
)
}
Next, we will take a quick look at our Spock specifications.
Introduction to Our Spock Specifications
Our example project has two simple specification classes that are described in the following:
First, the MessageServiceSpec class specifies one unit test that belongs to the UnitTest category. Its source code looks as follows:
import org.junit.experimental.categories.Category
import spock.lang.Specification
@Category(UnitTest.class)
class MessageServiceSpec extends Specification {
def messageService = new MessageService()
def 'Get message'() {
expect: 'Should return the correct message'
println 'Should return the correct message'
messageService.getMessage() == 'Hello World!'
}
}
The UnitTest interface is a simple marker interface that identifies tests which should be run when we run our unit tests. Its source code looks as follows:
interface UnitTest {
}
Second, the UnCategorizedSpec class specifies one unit test that doesn’t belong to any category. In other words, this unit test shouldn’t be run when we run our unit tests. The source code of the UnCategorizedSpec class looks as follows:
import spock.lang.Specification
class UnCategorizedSpec extends Specification {
def messageService = new MessageService()
def 'Should not be run'() {
expect: 'Should return the correct message'
println 'Should not be run'
messageService.getMessage() == 'Hello World!'
}
}
That being said, it’s important to understand that we print information to System.out only because it’s the easiest way to see which test methods are run. You shouldn’t do this in a real software project.
Let’s move on and find out how we can compile our unit tests with Gradle.
Compiling Our Unit Tests
Because our unit tests use the Groovy programming language, we have to compile them by using the Groovy plugin. Before we can use this plugin, we have apply the Groovy plugin.
After we have applied the Groovy plugin, our build.gradle file looks as follows:
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile(
'org.slf4j:slf4j-api:1.7.25',
'org.slf4j:slf4j-log4j12:1.7.25',
'log4j:log4j:1.2.17'
)
testCompile(
'junit:junit:4.12',
'org.codehaus.groovy:groovy-all:2.4.11',
'org.spockframework:spock-core:1.1-groovy-2.4'
)
}
Because the Groovy plugin extends the Java plugin and adds Groovy related tasks, dependency configurations, directories, and configuration properties to our Gradle build, we don’t have to configure any additional source or resource directories. The directories that are relevant for this lesson are described in the following:
- The src/main/java directory contains the source code of our application.
- The src/main/resources directory contains the resources of our application.
- The src/test/groovy directory contains the source code of our unit tests.
- The src/test/resources directory contains the resources of our unit tests.
Next, we will find out how we can ensure that Gradle runs only the tests which belong to the UnitTest category.
Configuring the Category of Our Unit Tests
When we want to configure the category of the invoked unit tests, we have to make these changes to the configuration of the test task:
- Enable JUnit support. Even though JUnit is enabled by default, we have to do this because we want to make changes to the JUnit configuration.
- Set the fully qualified class name of the
UnitTestinterface as the value of theincludeCategoriesJUnit configuration option. - Ensure that Gradle prints the information that is written to
System.outorSystem.err. This is not required, but if we don’t do this, we cannot see if Gradle runs only the unit tests which belong to theUnitTestcategory.
After we have configured the category of our unit tests, the source code of the build.gradle file looks as follows:
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile(
'org.slf4j:slf4j-api:1.7.25',
'org.slf4j:slf4j-log4j12:1.7.25',
'log4j:log4j:1.2.17'
)
testCompile(
'junit:junit:4.12',
'org.codehaus.groovy:groovy-all:2.4.11',
'org.spockframework:spock-core:1.1-groovy-2.4'
)
}
test {
useJUnit {
includeCategories 'com.testwithspring.master.UnitTest'
}
testLogging {
showStandardStreams = true
}
}
Let’s move on and find out how we can run our unit tests with Gradle.
Running Our Unit Tests With Gradle
We can run our unit tests by running the following command at command prompt:
gradle clean test
When we run this command, we notice that Gradle runs only the test that is found from the MessageServiceSpec class and ignores the test that is found from the UnCategorizedSpec class:
$ gradle clean test
> Task :test
com.testwithspring.master.MessageServiceSpec > Get message STANDARD_OUT
Should return the correct message
BUILD SUCCESSFUL in 2s
6 actionable tasks: 6 executed
Let’s summarize what we learned from this lesson.
Summary
This lesson has taught us three things:
- We can compile our unit tests by using the Groovy plugin.
- Because the Groovy plugin extends the Java plugin, we don’t have to configure any additional source or resource directories.
- We can configure the used JUnit 4 category by setting the value of the
includeCategoriesJUnit configuration option.