An example of testing Spring Boot application with Spock framework
Latest commit 50dd99c Feb 7, 2017 @int128 committed on GitHub Fix badge URL
Failed to load latest commit information.
gradle/wrapper Import minimum code Dec 12, 2016
src Add test class to verify reuse of application context through tests Feb 7, 2017
.gitignore Import minimum code Dec 12, 2016
.travis.yml Use Java 8 Dec 12, 2016
LICENSE Fix badge URL Feb 7, 2017
build.gradle Bump to Spring Boot 1.5 Feb 7, 2017
gradlew Import minimum code Dec 12, 2016
gradlew.bat Import minimum code Dec 12, 2016

Spock and Spring Boot example Build Status

This is an example of Spring Boot app tested with Spock framework.

  • Spring Boot 1.5
  • Spock 1.1-rc-3
  • Groovy 2.4

Component Test

These are component tests using the constructor mock injection.

Mark as a Spring Boot test without the web environment:

@SpringBootTest(webEnvironment = NONE)

Instantiate a service class with a mock dependency.

    @Subject BarService service

    ExternalApiClient client = Mock()

    def setup() {
        service = new BarService(client)

Declare the mock interaction by Spock style:

        1 * client.getDefault() >> new Hello('world')

Integration Test

These are end-to-end tests using the test rest template. See also Testing improvements in Spring Boot 1.4.

Mark as a Spring Boot test with the real web environment:

@SpringBootTest(webEnvironment = RANDOM_PORT)

Import the mock configuration.


Use the TestRestTemplate to make a request.

    TestRestTemplate restTemplate

Make a request to the target application:

        def entity = restTemplate.getForEntity('/foo', Hello)

Verify the response:

        entity.statusCode == HttpStatus.OK == 'world'