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:

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.

Let’s move on and find out how we can compile our unit tests with Maven.

Compiling Our Unit Tests

Because our unit tests use the Groovy programming language, we have to compile our test sources by using the GMavenPlus plugin. Before we can use this plugin, we have to declare this plugin in the plugins section of our POM file.

After we have done this, the relevant part of our pom.xml file looks as follows:

Running Our Unit Tests

We can run our unit tests by using the Maven Surefire Plugin. Before we can use this plugin, we have to declare it in the plugins section of our POM file. After we have done this, the relevant part of pom.xml file looks as follows:

After we have added this plugin to our Maven build, we have to configure the Maven Surefire Plugin by following these steps:

First, Ensure that it runs only the unit tests which belong to the UnitTest category. After we have made this change to the configuration of the Maven Surefire Plugin, the relevant part of our POM file looks as follows:

Second, the default configuration of the Maven Surefire Plugin includes all test classes whose name ends with the Strings: ‘Test’, ‘Tests’, or ‘TestCase’.

When I write automated tests with Spock Framework, I name my specification classes by appending the String: ‘Spec’ to the name of the specification class. That’s why we have to configure the Maven Surefire Plugin to include only test classes whose name ends with the String: ‘Spec’.

After we have configured the included test cases, the relevant part of our POM file looks as follows:

We can now run our unit tests by running the following command at command prompt:

mvn clean test

When we run this command, we see that the Maven Surefire Plugin runs only the unit test that is found from the MessageServiceSpec class and ignores the test that is found from the UnCategorizedSpec class: