O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Master Java Unit Testing with Spring Boot and Mockito

Video Description

Learn to Write Awesome Java JUnit Unit Tests with Spring Boot and Mockito in 40 Easy Steps

About This Video

  • Learn to write independent unit tests for RESTful web services talking with multiple layers - web, business, and data.
  • Learn how to write integration tests using an in-memory database H2.

In Detail

Spring Boot is the most popular framework to develop RESTful services. It has awesome unit testing capabilities through 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 (dependencies 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 learn to use the most important unit testing annotations - @RunWith(SpringRunner.class), @SpringBootTest, @WebMvcTest, @DataJpaTest and @MockBean.

Table of Contents

  1. Chapter 1 : Introduction
    1. Introduction 00:03:24
    2. Course Overview 00:02:12
  2. Chapter 2 : Mocking With Mockito
    1. Step 00 - Section Introduction - Mocking with Mockito 00:01:32
    2. Step 01 - Setting up the project using Spring Initializr 00:02:56
    3. Step 02 - Writing Unit Test for a Simple Business Service 00:06:03
    4. Step 03 - Setting up a Business Service to call a Data Service 00:03:49
    5. Step 04 - Writing your first unit test with Stub 00:04:56
    6. Step 05 - Exercise Solution - Updating Tests 2 & 3 to use Stubs 00:03:47
    7. Step 06 - Writing Unit Tests with Mocking using Mockito 00:05:38
    8. Step 07 - Exercise Solution - Updating Tests 2 & 3 to use Mockito 00:04:11
    9. Step 08 - More Refactoring - @Mock, @InjectMocks and @RunWith(MockitoJUnitRunner 00:03:25
    10. Step 09 - Mockito Tips - Multiple Return Values and Specific Argument Matchers 00:05:52
    11. Step 10 - Mockito Tips - Argument Matchers 00:02:58
    12. Step 11 - Mockito Tips - Verify method calls 00:06:40
    13. Step 12 - Mockito Tips - Argument Capture 00:04:04
    14. Step 13 - Mockito Tips - Argument Capture on Multiple Calls 00:02:33
    15. Step 14 - Introduction to Spy 00:07:42
    16. Step 15 - Mockito FAQ 00:03:24
  3. Chapter 3 : Unit Testing With Spring Boot and Mockito
    1. Step 00 - Section Introduction - Unit Testing with Spring Boot and Mockito 00:02:46
    2. Step 01 - Creating a Hello World Controller 00:03:40
    3. Step 02 - Using Mock Mvc to test Hello World Controller 00:12:40
    4. Step 03 - Using Response Matchers to check status and content 00:05:07
    5. Step 04 - Creating a Basic REST Service in Item Controller 00:05:08
    6. Step 05 - Unit Testing Item Controller and Basic JSON Assertions 00:04:05
    7. Step 06 - Digging deeper into JSON Assert 00:12:26
    8. Step 07 - Writing a REST Service talking to Business Layer 00:04:10
    9. Step 08 - Writing Unit Test for REST Service mocking Business Layer 00:06:51
    10. Step 09 - 00 - Overview of Steps 09 to 15 00:01:41
    11. Step 09 - 01 - Prepare Data Layers with JPA, Hibernate and H2 00:03:24
    12. Step 10 - Create Item Entity and Populate data with data.sql 00:06:00
    13. Step 11 - Create a RESTful Service talking to the database 00:07:54
    14. Step 12 - Writing Unit Test for Web Layer - Controller - Using Mock MVC 00:06:11
    15. Step 13 - Exercise & Solution - Writing Unit Test for Business Layer – Mocking 00:05:27
    16. Step 14 - Writing Unit Test for Data Layer - Data JPA Test 00:08:01
    17. Step 15 - Writing an Integration Test using @SpringBootTest 00:08:51
    18. Step 16 - Tip - Using @MockBean to mock out dependencies you do not want to talk 00:05:03
    19. Step 17 - Tip - Creating Different Test Configuration 00:03:40
    20. Step 18 - Writing Unit Tests for Other Request Methods 00:03:28
    21. Step 19 - Refactor SomeBusinessImpl to use Functional Programming 00:03:49
    22. Step 20 - Better Assertions with Hamcrest – HamcrestMatcherTest 00:08:37
    23. Step 21 - Better Assertions with AssertJ – AssertJTest 00:06:27
    24. Step 22 - Better Assertions with JSONPath – JSONPathTest 00:09:38
    25. Step 23 - Tip Patterns – xunitpatterns 00:01:22
    26. Step 24 - Tip - Measuring Test Coverage with Eclipse 00:04:25
    27. Step 25 - Tip - Keep an eye on performance of unit tests! 00:03:03
    28. Step 26 - Good Unit Tests 00:05:34
  4. Chapter 4 : Congratulations
    1. Next Steps 00:00:50