Spring Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:


1. Introduction

In this short tutorial, we’ll show the benefits of combining the supporting power of Spring Boot‘s testing framework and the expressiveness of the Spock framework whether that be for unit or integration tests.

2. Project Setup

Let’s start with a simple web application. It can greet, change the greeting and reset it back to the default by simple REST calls.  Aside from the main class, we use a simple RestController to provide the functionality:

public class WebController {

    public String salutation() {
        return "Hello world!";

So the controller greets with ‘Hello world!’. The @RestController annotation and the shortcut annotations ensure the REST endpoint registration.

3. Maven Dependencies for Spock and Spring Boot Test

We start by adding the Maven dependencies and if needed Maven plugin configuration.

3.1. Adding the Spock Framework Dependencies with Spring Support

For Spock itself and for the Spring support we need two dependencies:



Notice, that the versions are specified with are a reference to the used groovy version.

3.2. Adding Spring Boot Test

In order to use the testing utilities of Spring Boot Test, we need the following dependency:


3.3. Setting up Groovy

And since Spock is based on Groovy, we have to add and configure the gmavenplus-plugin as well to be able to use this language in our tests:


Note, that since we only need Groovy for test purposes and therefore we restrict the plugin goal to compileTest.

4. Loading the ApplicationContext in a Spock Test

One simple test is to check if all Beans in the Spring application context are created:

class LoadContextTest extends Specification {

    @Autowired (required = false)
    private WebController webController

    def "when context is loaded then all expected beans are created"() {
        expect: "the WebController is created"

For this integration test, we need to start up the ApplicationContext, which is what @SpringBootTest does for us. Spock provides the section separation in our test with the keywords like “when”, “then” or “expect”.

In addition, we can exploit Groovy Truth to check if a bean is null, as the last line of our test here.

5. Using WebMvcTest in a Spock Test

Likewise, we can test the behavior of the WebController:

class WebControllerTest extends Specification {

    private MockMvc mvc

    def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
        expect: "Status is 200 and the response is 'Hello world!'"
          .contentAsString == "Hello world!"

It’s important to note that in our Spock tests (or rather Specifications) we can use all familiar annotations from the Spring Boot test framework that we are used to.

6. Conclusion

In this article, we’ve explained how to set up a Maven project to use Spock and the Spring Boot test framework combined. Furthermore, we have seen how both frameworks supplement each other perfectly.

For a deeper dive, have a look to our tutorials about testing with Spring Boot, about the Spock framework and about the Groovy language.

Finally, the source code with additional examples can be found in our GitHub repository.

Spring bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring:


Leave a Reply

Notify of