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:

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...

6 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.

  2. Milin says:

    I followed the steps it says Empty test suite. in intellij. help ?

    • Ignacio Suay says:

      I remember having a similar issue but I can’t remember how I solved it 🙁

      If you run the test from terminal with mvn test, does it run the tests?

      In intellij, have you marked your test folder as “Test source root”? if not, try doing right click in your src/test/groovy folder, an then mark directory as -> Mark as Test source root.

      Please let me know if you manage to fix this issue

  3. Abhijeet Pandey says:

    I’ve tried this but autowired object still coming as null.Can you help me?

  4. jewe; says:

    Is this an example integration test or a unit test?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.