Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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>2.4-M4-groovy-4.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>2.4-M4-groovy-4.0</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>3.2.1</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>3.0.2</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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW