Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Spring Boot Cookbook

You're reading from   Spring Boot Cookbook Over 35 recipes to help you build, test, and run Spring applications using Spring Boot

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher Packt
ISBN-13 9781785284151
Length 206 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alex Antonov Alex Antonov
Author Profile Icon Alex Antonov
Alex Antonov
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Getting Started with Spring Boot FREE CHAPTER 2. Configuring Web Applications 3. Web Framework Behavior Tuning 4. Writing Custom Spring Boot Starters 5. Application Testing 6. Application Packaging and Deployment 7. Health Monitoring and Data Visualization Index

Creating tests using Mockito to mock DB


Now that we are done with setting up the fixtures to use during the set up and population of our database when it is used for testing purposes, let's examine how we can use the power of Mockito so that we don't need to rely on the database at all. We will learn how to elegantly mock the Repository instance objects using the Mockito framework and some annotation cleverness.

We will create a special Spring Boot configuration class where we will define and replace the Spring Beans that are necessary for testing purposes. We will use annotations to tell Spring Boot when to add the testing configuration and when to exclude it. In the configuration class, we will use Mockito to create some mock objects with preconfigured behavior, which will later be injected by Spring Boot while executing the tests.

How to do it…

  1. First, we will create a special annotation that we will use in order to tag the configuration classes that are designed to be loaded for testing...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image