Spring Boot 2.x and Mocks

Image for post
Image for post

Spring Boot has interesting extensions for mocks using Mockito. Sometimes mocks can be very harried. Unit Tests make sense for services however the cost of mocks sometimes is too high and it could be a very time-consuming and complicated task. Spring-Boot has interesting support together with mockito to make mocks easier and more declarative. Mock is not a free lunch dont get me wrong. However, when you are coding a service Dependency Injection makes sense. Since Dependency Injection makes sense for services why such a thing would not make sense for mocks? Well, it’s the same principle. If your implementation relies on dependency injection, your tests should also do the same. I have a bit of different opinion about shared libraries where I dont think they should be coupled with Spring and therefore might not have DI at All. Libraries and Services are completely different animals. Going back to services, today I want to share a video and some sample code I made to show how mocking can be simplified by Spring and Mockito working together. Let’s get started.

The Video

The Code


Diego Pacheco

Originally published at http://diego-pacheco.blogspot.com on January 10, 2021.

Brazilian, Software Architect, SWE(Java, Scala, Rust, Go) SOA & DevOps expert, Author. Working with EKS/K8S. diegopacheco.github.io (Opinions on my own)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store