Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Integration Testing with Spock

DZone's Guide to

Spring Integration Testing with Spock

· Integration Zone
Free Resource

Discover how Microservices are a type of software architecture where large applications are made up of small, self-contained units working together through APIs that are not dependent on a specific language. Brought to you in partnership with AppDynamics.

I'm currently working on a project where we use Spring Framework 4.0 and Spock for testing. Unit testing in Spock is really great, but there are some lacks in integration testing - especially in mocking Spring beans. Our requirement is to start up Spring context but with particular bean replaced with mock.

The easiest part we had do implement is just regular Spring test specification:

@ContextConfiguration(
    classes = [GatewayConfig, PaymentServiceConfig])
class PaymentServiceSpecIT extends Specification {

  final String CLIENT_ID = "54321"

  @Inject
  PaymentService paymentService

  def "Should return auth token"() {
    when:
      def token = paymentService.getAuthToken(CLIENT_ID)
    then:
      token
  }
}


The problem here is that getAuthToken method have to find this client configuration (including for example some keys data). It's done by invoking configurationService.getClientConfig(CLIENT_ID) method. So the best option here is to mock this invocation and return some predefined fixture.

What comes on my mind in that moment is Springockito project. Due to JavaConfig is our application springockito-annotations module have to be used. After adding proper dependency you just need to add "loader = SpringockitoAnnotatedContextLoader" into @ContextConfiguration.

And last but not least part - setting up mock. After many tries it was impossible for me to drive Spock mocking framework to do that, so I come back to old-school Mockito. Finally my test class looks as follows:

@ContextConfiguration(
    classes = [GatewayConfig, PaymentServiceConfig],
    loader = SpringockitoAnnotatedContextLoader)
class PaymentServiceSpecIT extends Specification {

  final String CLIENT_ID = "54321"

  @Inject
  PaymentService paymentService

  @Inject
  @ReplaceWithMock
  ConfigurationService configService

  def "Should return auth token"() {
    given:
      given(configService.getClientConfig(CLIENT_ID))
          .willReturn(DataFixtures.clientConfig())
    when:
      def token = paymentService.getAuthToken(CLIENT_ID)
    then:
      token
  }
}


And that leads our test to working properly :)

Discover the six challenges and best practices in managing microservice performance, brought to you in partnership with AppDynamics.

Topics:
java ,frameworks ,testing ,spock

Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}