Test With Spring Course
Save Time by Writing Less Test Code
  • Home
  • Pre-Sales FAQ
  • Support
  • Log In

/ June 26, 2017 / petrikainulainen

Running Unit Tests With Gradle – Spock Edition

Lesson Progress:
← Back to Topic

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.

This lesson assumes that:

  • You are familiar with JUnit 4 categories
  • You can run unit tests with Gradle

Watch the Lesson

 

The text version of this lesson is given in the following:

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 UnitTest category.

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'
    )
}

We don’t have to add the JUnit and Groovy dependencies to our build.gradle file. However, if we leave them out, we have to use the JUnit and Groovy versions specified by Spock Core dependency.

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!'
    }
}

By the way, we will take a closer look at Spock specifications in the next lessons of this topic. In other words, you shouldn’t worry if you don’t understand what these specification classes are doing.

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.

Additional Reading:

  • Gradle User Guide: Chapter 55. The Groovy Plugin

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:

  1. 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.
  2. Set the fully qualified class name of the UnitTest interface as the value of the includeCategories JUnit configuration option.
  3. Ensure that Gradle prints the information that is written to System.out or System.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 the UnitTest category.

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
    }
}

Additional Reading:

  • Gradle User Guide: Example 47.13. JUnit Categories

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 includeCategories JUnit configuration option.

Get the source code from Github

← Previous Lesson Next Lesson →

Can I help you?

This is a free sample lesson of my Test With Spring course. If this lesson helped you to solve your problem, you should find out how my testing course can help you.

Support and Privacy

  • Pre-Sales FAQ
  • Support
  • Cookie Policy
  • No Bullshit Privacy Policy
  • No Bullshit Terms and Conditions

Test With Spring Course

  • Starter Package
  • Intermediate Package
  • Master Package

Free Sample Lessons

  • Introduction to JUnit 4
  • Introduction to Unit Testing
  • Introduction to Integration Testing
  • Introduction to End-to-End Testing
  • Introduction to Spock Framework
  • Introduction to Integration Testing – Spock Edition
  • Writing End-to-End Tests With Spock Framework

Copyright Koodikupla Oy 2016 — Built on Thesis by Themedy