Skip to content

ltl3884/spock-spring-boot-example

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

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

@Import(IntegrationTestConfiguration)

Use the TestRestTemplate to make a request.

    @Autowired
    TestRestTemplate restTemplate

Make a request to the target application:

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

Verify the response:

        then:
        entity.statusCode == HttpStatus.OK
        entity.body.name == 'world'

About

An example of testing Spring Boot application with Spock framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Groovy 100.0%