Book Image

Building Web Apps with Spring 5 and Angular

By : Ajitesh Kumar Shukla
Book Image

Building Web Apps with Spring 5 and Angular

By: Ajitesh Kumar Shukla

Overview of this book

Spring is the most popular application development framework being adopted by millions of developers around the world to create high performing, easily testable, reusable code. Its lightweight nature and extensibility helps you write robust and highly-scalable server-side web applications. Coupled with the power and efficiency of Angular, creating web applications has never been easier. If you want build end-to-end modern web application using Spring and Angular, then this book is for you. The book directly heads to show you how to create the backend with Spring, showing you how to configure the Spring MVC and handle Web requests. It will take you through the key aspects such as building REST API endpoints, using Hibernate, working with Junit 5 etc. Once you have secured and tested the backend, we will go ahead and start working on the front end with Angular. You will learn about fundamentals of Angular and Typescript and create an SPA using components, routing etc. Finally, you will see how to integrate both the applications with REST protocol and deploy the application using tools such as Jenkins and Docker.
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Unit testing controllers, services, and DAOs


In this section, we will take a look at how to perform unit testing with components such as controllers, services, and DAOs. We shall also look at some of the features of JUnit 5.

Unit tests for controllers

Let's understand some of the following concepts before we get to look at the source codes for controllers and the corresponding unit tests written using JUnit 5:

  • MockMvc: This is a Spring MVC component primarily used for writing unit tests with controllers components. Simply speaking, MockMvc is used to mock the Spring MVC container. The following is what the code looks like when using MockMvc:
        private MockMvc mockMvc;

        @MockBean
        private UserService userService;

        @BeforeEach
        public void setUp() throws Exception {
 this.mockMvc = MockMvcBuilders.standaloneSetup(new      
        UserAccountController(this.userService)).build();
        }

In the preceding code, UserService, a dependency within the controller...