Spring integration testing with Spock

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:

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

  final String CLIENT_ID = "54321"

  PaymentService paymentService

  def "Should return auth token"() {
            def token = paymentService.getAuthToken(CLIENT_ID)


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:

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

  final String CLIENT_ID = "54321"

  PaymentService paymentService

  ConfigurationService configService

  def "Should return auth token"() {
      def token = paymentService.getAuthToken(CLIENT_ID)


And that leads our test to working properly :)


dsrini.open said…
How did you setup the contextconfig, the other files. Is it through spring beans xml file ? Can you show that configs ?
Jakub Kubrynski said…
Spring configuration is done by normal @Configuration classes and there is nothing specific related to tests
pbetkier said…
Helpful post, integrating Spring, Spock and Springockito can be tricky.

It's worth noting that there is a pitfall when integrating Spock and Spring Boot with implicit property sources (application.properties etc.). In order to get them loaded in the first example (without Springockito), we should additionally specify "loader = SpringApplicationContextLoader" in @ContextConfiguration. In the second example, with Springockito, we cannot do that since we need a SpringockitoAnnotatedContextLoader, but adding "initializers = ConfigFileApplicationContextInitializer" to @ContextConfiguration solves the problem.

Alternatively, we could define our property sources explicitly in our JavaConfig e.g. using @PropertySource("application.properties") and leave @ContextConfigurations as presented.

Do you have a better suggestion on how to overcome this pitfall?
Jakub Kubrynski said…
IMHO as long as Spock doesn't support meta-annotations there is no better solution for that
Karol Kuc said…
Why not just
ConfigurationService mockedConfiguratinService(){
ConfigurationService cs = Mock()
in a configuration class?
Jakub Kubrynski said…
The problem with Spock mocks is that when you want to verify them it needs to be in the same line as setup. But I currently use constructions you've posted with Mockito, however the initialization is done in the particular test, because I don't like to keep mocking separately from test. That causes the problem that you have to remember what is a mock and what is not. It's easy when you're mocking whole layers (as described here: http://www.kubrynski.com/2015/11/smart-package-structure-to-improve.html)
Hello Jakub,
The Article on Spring integration testing with Spock is amazing give detail information about it .Thanks for Sharing the information about Integration Testing.
Software Testing Company
suvathi said…
Such an awesome article, these many days, doubts in multiple concepts were bothering me, I started searching through books and net, but doubt was not cleared, thank you so much for providing this article, helped me a lot
software testing training in chennai
ajay ak said…
Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
Software testing course in chennai
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.

And also those who are looking for
Web Designing Training Institute in Chennai
SEO Training Institute in Chennai
Photoshop Training Institute in Chennai
PHP & Mysql Training Institute in Chennai
Android Training Institute in Chennai
Rigid Box said…
Excellent Blog. I really want to admire the quality of this post. I like the way of your presentation of ideas, views and valuable content. No doubt you are doing great work. I’ll be waiting for your next post. Thanks .Keep it up!
Kindly visit us @
Luxury Boxes
Premium Packaging
Luxury Candles Box
Earphone Packaging Box
Wireless Headphone Box
Innovative Packaging Boxes
Wedding gift box
Leather Bag Packaging Box
Cosmetics Packaging Box
Luxury Chocolate Boxes
adhi said…
thanks for your information really good and very nice web design company in velachery
zuan said…
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
web designing classes in chennai | web designing training institute in chennai
web designing and development course in chennai | web designing courses in Chennai
best institute for web designing in chennai | web designing course with placement in chennai

Popular posts from this blog

Understanding the first level JPA cache

Including Java agent in standalone Spring Boot application

Understanding Spring Web Initialization