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:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.1-groovy-2.4-rc-2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>1.1-groovy-2.4-rc-3</version>
        </dependency>
        <!-- Optiona: Spock genesis for generating data -->
        <dependency>
            <groupId>com.nagternal</groupId>
            <artifactId>spock-genesis</artifactId>
            <version>0.5.0</version>
        </dependency>
    </dependencies>

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 {

    @Autowired
    MovieRepository movieRepository;

    @Autowired
    MovieService movieService;

    @Unroll
    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

        cleanup:
        movieRepository.delete(movie)

        where:
        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 *