Book Image

Spring Boot Cookbook

By : Alex Antonov
Book Image

Spring Boot Cookbook

By: Alex Antonov

Overview of this book

Table of Contents (15 chapters)
Spring Boot Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
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...