Master Java Unit Testing with Spring Boot and Mockito

Video description

Spring Boot is the most popular framework to develop RESTful services. It has awesome unit testing capabilities through the Spring Boot Starter Test. Mockito is the most popular mocking framework. JUnit is the most popular Java unit testing framework. You will build the unit tests step by step (in 40 easy steps).

This course would be a perfect first step as an introduction to unit testing with the Spring Boot and Mockito frameworks. You will be using Spring (dependency management), Spring Boot, Maven (dependency management), Eclipse (IDE), in-memory database H2 and Tomcat embedded web server. We will help you set up each one of these. You will use all the frameworks that are part of the Spring Boot Starter Test: JUnit, Spring Test, Spring Boot Test, AssertJ, Hamcrest, Mockito, JSONassert, and JSONPath. You will also learn how to use the most important unit testing annotations: @RunWith(SpringRunner.class), @SpringBootTest, @WebMvcTest, @DataJpaTest, and @MockBean.

By the end of this course, you will be able to write awesome unit tests for your RESTful services.

What You Will Learn

  • Write great unit and integration tests using Spring Boot Starter Test
  • Write unit tests using Mocks and Spys created with Mockito
  • Use Mock Mvc to test hello world controller
  • Use the most important unit testing annotations
  • Learn and use all frameworks of Spring Boot Starter Test
  • Write integration tests using an in-memory database, H2

Audience

You are a Spring / Spring Boot / Java programmer, and you want to write awesome unit tests for your RESTful services. You want to write awesome unit tests with mocking. You want to understand the best practices in writing great unit tests with JUnit, Mockito, and Spring Boot Starter Test. You want to learn how to write great asserts with Hamcrest Matchers, JSONassert, Assert J, and JSON Path libraries.

About The Author

In28Minutes Official: Ranga Karanam is a seasoned technologist and architect with over two decades of expertise in programming, design, and architecture. He founded in28Minutes with the goal of helping students master cutting-edge cloud-native technologies such as AWS, Azure, Google Cloud, Docker, and Kubernetes. With 15 years of experience in Java programming and design, Ranga has collaborated with top banking clients worldwide.

His passion for creating hands-on courses with real-world projects motivated him to develop the Step-By-Step series of courses that follow a problem-solution-based approach, with practical and real-world application examples.

Ranga and his team at in28Minutes specialize in Java and related frameworks such as Spring, Spring Boot, Spring MVC, Struts, and Hibernate. They are committed to equipping learners with the skills and knowledge necessary to succeed in today’s rapidly evolving technology landscape.

Table of contents

  1. Chapter 1 : Introduction
    1. Introduction
    2. Course Overview
  2. Chapter 2 : Mocking with Mockito
    1. Step 00 - Section Introduction - Mocking with Mockito
    2. Step 01 - Setting Up the Project Using Spring Initializr
    3. Step 02 - Writing a Unit Test for a Simple Business Service
    4. Step 03 - Setting Up a Business Service to call a Data Service
    5. Step 04 - Writing Your First Unit Test with Stub
    6. Step 05 - Exercise Solution - Updating Tests 2 and 3 to Use Stub
    7. Step 06 - Writing Unit Tests with Mocking Using Mockito
    8. Step 07 - Exercise Solution - Updating Tests 2 and 3 to Use Mockito
    9. Step 08 - More Refactoring - @Mock, @InjectMocks, and @RunWith(MockitoJUnitRunner)
    10. Step 09 - Mockito Tips - Multiple Return Values and Specific Argument Matchers
    11. Step 10 - Mockito Tips - Argument Matchers
    12. Step 11 - Mockito Tips - Verify Method Calls
    13. Step 12 - Mockito Tips - Argument Capture
    14. Step 13 - Mockito Tips - Argument Capture on Multiple Calls
    15. Step 14 - Introduction to Spy
    16. Step 15 - Mockito FAQs
  3. Chapter 3 : Unit Testing with Spring Boot and Mockito
    1. Step 00 - Section Introduction - Unit Testing with Spring Boot and Mockito
    2. Step 01 - Creating a Hello World Controller
    3. Step 02 - Using MockMvc to Test the Hello World Controller
    4. Step 03 - Using Response Matchers to Check Status and Content
    5. Step 04 - Creating a Basic REST Service in Item Controller
    6. Step 05 - Unit Testing Item Controller and Basic JSON Assertions
    7. Step 06 - Digging Deeper into JSONassert
    8. Step 07 - Writing a REST Service talking to Business Layer
    9. Step 08 - Writing Unit Test for REST Service Mocking Business Layer
    10. Step 09 - 00 - Overview of Steps 09 to 15
    11. Step 09 - 01 - Prepare Data Layers with JPA, Hibernate, and H2
    12. Step 10 - Create Item Entity and Populate Data with data.sql
    13. Step 11 - Create a RESTful Service Talking to the Database
    14. Step 12 - Writing Unit Test for Web Layer - Controller - Using MockMvc
    15. Step 13 - Exercise and Solution - Writing Unit Test for Business Layer ? Mocking
    16. Step 14 - Writing Unit Test for Data Layer - Data JPA Test
    17. Step 15 - Writing an Integration Test Using @SpringBootTest
    18. Step 16 - Tip - Using @MockBean to Mock Out Dependencies that are Not Required
    19. Step 17 - Tip - Creating Different Test Configuration
    20. Step 18 - Writing Unit Tests for Other Request Methods
    21. Step 19 - Refactor SomeBusinessImpl to Use Functional Programming
    22. Step 20 - Better Assertions with Hamcrest ? HamcrestMatcherTest
    23. Step 21 - Better Assertions with AssertJ ? AssertJTest
    24. Step 22 - Better Assertions with JSONPath ? JSONPathTest
    25. Step 23 - Tip Patterns ? xunitpatterns
    26. Step 24 - Tip - Measuring Test Coverage with Eclipse
    27. Step 25 - Tip - Keep an Eye on Performance of Unit Tests!
    28. Step 26 - Good Unit Tests
  4. Chapter 4 : Congratulations
    1. Next Steps

Product information

  • Title: Master Java Unit Testing with Spring Boot and Mockito
  • Author(s): In28Minutes Official
  • Release date: May 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789346077