Book Image

Developing Java Applications with Spring and Spring Boot

By : Claudio Eduardo de Oliveira, Greg L. Turnquist, Alex Antonov
Book Image

Developing Java Applications with Spring and Spring Boot

By: Claudio Eduardo de Oliveira, Greg L. Turnquist, Alex Antonov

Overview of this book

Spring Framework has become the most popular framework for Java development. It not only simplifies software development but also improves developer productivity. This book covers effective ways to develop robust applications in Java using Spring. The course is up made of three modules, each one having a take-away relating to building end-to-end java applications. The first module takes the approach of learning Spring frameworks by building applications.You will learn to build APIs and integrate them with popular fraemworks suh as AngularJS, Spring WebFlux, and Spring Data. You will also learn to build microservices using Spring's support for Kotlin. You will learn about the Reactive paradigm in the Spring architecture using Project Reactor. In the second module, after getting hands-on with Spring, you will learn about the most popular tool in the Spring ecosystem-Spring Boot. You will learn to build applications with Spring Boot, bundle them, and deploy them on the cloud. After learning to build applications with Spring Boot, you will be able to use various tests that are an important part of application development. We also cover the important developer tools such as AMQP messaging, websockets, security, and more. This will give you a good functional understanding of scalable development in the Spring ecosystem with Spring Boot. In the third and final module, you will tackle the most important challenges in Java application development with Spring Boot using practical recipes. Including recipes for testing, deployment, monitoring, and securing your applications. This module will also address the functional and technical requirements for building enterprise applications. By the end of the course you will be comfortable with using Spring and Spring Boot to develop Java applications and will have mastered the intricacies of production-grade applications.
Table of Contents (34 chapters)
Title Page - Courses
Copyright and Credits - Courses
Packt Upsell - Courses
Preface
Bibliography
Index

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.

How to do it...

  1. First, we will create a new MockPublisherRepositoryTests test class in the src/test/java/com/example/bookpub directory...