How to do integration tests with Spring Boot and Spock

In the latest version of Spring Boot (1.4) they have introduced a new annotation called @SpringBootTest. Previous to this version you may have used either @ContextConfiguration or @SpringApplicationConfiguration combined with @IntegrationTest or/and @WebIntegrationTest to run your integration tests. Nevertheless, in the latest version you could use the single annotation @SpringBootTest and by default spring will load the default profile and will load the configuration from the primary @SpringBootApplication class.

In order to run a spock test which involves spring, all you will need to do is follow these 3 steps:

1) In your pom file you should include Spring boot (version > 1.4), Spock and spring-spock:


        <!-- Optiona: Spock genesis for generating data -->

2) Create a class which extends the spock.lang.Specification class

3) Add the @SpringBootTest annotation including your configuration classes or your primary @SpringBootApplication class.

That is all, now you should be able to autowired any dependency and use all the spring features.

Here is an example of an integration test which is responsible to test a movie service. This test uses Spock Genesis for generating 10 random movies. Then it will save it into the database and finally check that the movie exists in the database.

@SpringBootTest(classes = Application.class)
class MovieServiceSpockIT extends Specification {

    MovieRepository movieRepository;

    MovieService movieService;

    def "given a movie #movie.title save it into the database"() {
        when: "A movie is created"
        def movieSaved = movieService.createMovie(movie)

        then: "Check the movie is in the database with the correct values"
        def foundMovie = movieRepository.findOne(movieSaved.getId())
        foundMovie.title == movie.title
        foundMovie.director == movie.director


        movie << getMovie().take(10)

    Generator getMovie() {
        return Gen.type(Movie,
                title: Gen.string(20),
                director: getPerson()

    Generator getPerson() {
        return Gen.type(Person,
                name: Gen.string(10),
                birthday: Gen.string(10))

For any further information, please find this test in github.

You may also like...

2 Responses

  1. Andreas Guther says:

    Thank you for the informative post. Just a small remark: The annotation name is @SpringBootTest, i.e. singular, not plural @SpringBootTests as you wrote it. However, in your code the annotation is used correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *