Book Image

Spring Boot 2.0 Cookbook - Second Edition

By : Alex Antonov
Book Image

Spring Boot 2.0 Cookbook - Second Edition

By: Alex Antonov

Overview of this book

The Spring framework provides great flexibility for Java development, which also results in tedious configuration work. Spring Boot addresses the configuration difficulties of Spring and makes it easy to create standalone, production-grade Spring-based applications. This practical guide makes the existing development process more efficient. Spring Boot Cookbook 2.0 Second Edition smartly combines all the skills and expertise to efficiently develop, test, deploy, and monitor applications using Spring Boot on premise and in the cloud. We start with an overview of the important Spring Boot features you will learn to create a web application for a RESTful service. Learn to fine-tune the behavior of a web application by learning about custom routes and asset paths and how to modify routing patterns. Address the requirements of a complex enterprise application and cover the creation of custom Spring Boot starters. This book also includes examples of the new and improved facilities available to create various kinds of tests introduced in Spring Boot 1.4 and 2.0, and gain insights into Spring Boot DevTools. Explore the basics of Spring Boot Cloud modules and various Cloud starters to make applications in “Cloud Native” and take advantage of Service Discovery and Circuit Breakers.
Table of Contents (11 chapters)

Creating tests using mock objects

In the previous recipe, we used a data fixture file to populate an in-memory database in order to run our tests on predictable and static sets of data. While this makes the tests consistent and deterministic, we are still paying the price of having to create a database, populate it with data, and initialize all the JPA and connectivity components, which could be viewed as an excessive step for a test. Luckily, Spring Boot provides internal support for being able to mock beans and inject them as components in the tests for setup and further use as dependencies within an application context.

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 @MockBean annotation cleverness.

...