• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mockito book - question

 
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ken,

As you may know, good chunk of us here are using Spring in our daily life. Can you explain me how does content of your book can help in understanding Spring's Mockito wrapper around core Mockito?

I have very thin understanding of what you exactly are stubs/spies etc, when to used them etc. Seems to me that Im struggling more how to understand connection Spring-Mockito rather then Mockito iteself.

In short, does your book can help us using Mockito with Spring?
 
gunslinger & author
Posts: 169
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use Spring (and Spring Boot, and Spring Data, ...) all the time as well. One of the nice testing features of Spring is that it includes Mockito as a dependency whenever you make a Spring Boot app, so the framework is already there.

To support Mockito, Spring provides an annotation called . That tells the framework to create a mock of the annotated class and replace any instance in the application context with that mock. That makes your mock available for dependency injection.

One of the tasks involved when you use Mockito is that you need a way to plug your mocks into the class you're testing. In non-Spring apps, you usually provide a constructor argument or a setter for that purpose. With Spring, dependencies are "autowired" in, so that approach doesn't work. That's why Spring created their own annotation.

You can see an example of this in the Spring Boot documentation in this section: https://docs.spring.io/spring-boot/docs/3.0.2/reference/htmlsingle/#features.testing.spring-boot-applications.mocking-beans on Mocking and Spying Beans.

The rest of the concepts in Mockito (what are mocks, stubs, and spies; how do you use them; how do you set expectations, and so on) all work the same way in Spring that they do in non-Spring apps. My book covers those, and while it includes a section on Spring (specifically, Working with the Spring Framework in the chapter called Use Mockito in Special Cases), I mostly focus on the general concepts which work the same everywhere.
 
reply
    Bookmark Topic Watch Topic
  • New Topic