Spring Top

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

>> CHECK OUT THE COURSE

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:

@RestController
@RequestMapping("/hello")
public class WebController {

    @GetMapping
    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:

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>1.2-groovy-2.4</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.2-groovy-2.4</version>
    <scope>test</scope>
</dependency>

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:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.1.1.RELEASE</version>
    <scope>test</scope>
</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:

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <goals>
                <goal>compileTests</goal>
            </goals>
        </execution>
    </executions>
</plugin>

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:

@SpringBootTest
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"
        webController
    }
}

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:

@AutoConfigureMockMvc
@WebMvcTest
class WebControllerTest extends Specification {

    @Autowired
    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!'"
        mvc.perform(get("/hello"))
          .andExpect(status().isOk())
          .andReturn()
          .response
          .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:

>> CHECK OUT THE LESSONS

Leave a Reply

avatar
  Subscribe  
Notify of