
Learn to write JUnit tests with Spring Boot and Mockito, covering mocking and spying, web, business, and data layer tests using MockMvc and data JPA tests.
Explore Java unit testing with mocking using Mockito and test a simple multi-layer rest service with the Spring Boot test framework, covering unit tests for individual layers.
Master java unit testing with spring boot and Mockito by learning mocking and spying, building a simple spring initializer project, and testing with mocks and stubs.
Set up a new Spring Boot project with Spring Initializr, using Maven, Java, and Spring Boot 2.x, then add web and Spring Boot Starter Test dependencies for unit testing.
Understand the need for mocking and start with a simple unit test for a basic business service. Create a sum calculation, write JUnit tests with @Test and assert equals.
Learn how to wire a business service to call a data service and test it with Mockito, including creating a data service interface and injecting it via a setter.
Write your first unit test for calculate sum using data service by creating a stub that returns predefined data instead of a database, then run the JUnit test.
Explore updating tests to use stubs for a data service, addressing maintainability and compilation issues from multiple stub classes, and investigate programmatically creating classes as alternatives.
Learn to replace stubs with mocks using Mockito, creating a mock of a data service and defining behavior with when(...).thenReturn(...); run tests to verify that mocks return specified data.
Update tests to use Mockito and reduce duplication by moving common setup to a @BeforeEach method in JUnit tests, then inline values for readability while keeping mocks and tests passing.
Explore how to use @InjectMocks, @Mock, and @RunWith(MockitoJUnitRunner) to inject mocks, simplify unit tests, and mock the data service for cleaner test scenarios.
Discover practical Mockito tips by mocking a list, stubbing multiple return values, and using specific argument matchers like get(0) while noting default null for unknown parameters.
Learn to use Mockito argument matchers such as any int to return 28 minutes for any parameter, and explore various any methods.
Verify method calls with Mockito by using verify, argument matchers, and times controls (at least, at most, never) to ensure correct interactions in unit tests.
Master Java unit testing with Spring Boot and Mockito by learning to capture arguments passed to mock methods using an argument captor, verify calls, and assert the captured value.
Master argument capture across multiple Mockito calls, retrieving all values with get all values, verifying two invocations, and asserting each captured argument in a practical scenario.
Master how spies differ from mocks in Java unit testing. Retain real behavior while stubbing specific methods and verify interactions with the spy.
Explore the Mockito FAQ to understand limitations, including mocking classes and interfaces, default return values, and using PowerMock with JUnit four for static and private methods or constructors.
Minimize distractions, discover your productivity zone, and use the Pomodoro technique to boost focus and steady progress in mastering Java unit testing with Spring Boot and Mockito.
Explore unit testing of web, business, and data layers with Spring Boot and Mockito, using MockMvc and Json asserts, culminating in an integration test of all layers.
Create a simple Spring hello world controller with @RestController and a get mapping for /Hello world, run in an embedded Tomcat Spring Boot app, to prepare for unit testing.
Learn to unit test a hello world controller with Spring MockMvc, avoiding full app startup, by issuing a GET to /hello world and validating the response.
Master Java unit testing with mock mvc: learn to perform requests, use end expect matchers to verify status 200 and content, including simple strings or json.
Implement a rest controller that returns a dummy item as json, with id, name, price, and quantity, illustrating Spring Boot's automatic bean-to-json conversion.
Learn to unit test the item controller and perform basic json assertions using content().json for robust, whitespace-tolerant checks.
Explore Json assert in spring boot tests, compare actual json content to expected using Json assert equals with strict and non-strict modes, and see highlights of differences.
Write a unit test for the item controller by delegating to a mocked item business service, using autowired components and a hard coded item, before introducing database access.
Write a unit test for the item controller’s REST API by mocking the business service with Spring Boot’s MockBean, returning a hardcoded item to validate the response.
Outline steps 09 to 15, showing how to set up data layer with an in-memory database using JPA, Hibernate, and Spring Data JPA, and unit test web and data layers.
Add a data layer using JPA with Hibernate and an in-memory H2 database via Spring Boot Data JPA to enable quick data access for unit testing.
Create an item entity with a primary key and a transient value, enable sql logging, and populate an in-memory h2 database via data.sql, then verify the item table.
Create a restful service that reads all items from the database via a Spring Data JPA repository, exposes them through a controller, and applies price times quantity as value.
Write and run unit tests for the web layer controller using Mock MVC, stubbing the business service to return all items and validating the JSON response.
Create a unit test for the item business service by mocking the item repository and asserting the retrieve all items logic yields expected values.
Learn to write a data layer unit test with Spring Boot data JPA test for the item repository, using an in-memory database and data.sql to verify find all.
Write an integration test with Spring Boot test that launches the full application context, uses a random port, and calls the item controller with test rest template to validate JSON.
Learn how to run integration tests with a spring context while mocking external dependencies using @MockBean, avoiding real databases, and ensuring proper communication across web, business, and data layers.
Override application configurations for unit tests by using a dedicated test resources folder and test property sources, controlling h2 console visibility and sql output with prioritized properties.
Unit test get, post, put, and delete requests in spring boot with Mockito, verify statuses (including 201 and 404), post content, and assert response headers like location.
Refactor the business logic to use functional programming with streams, reducing values to a sum via a method reference and optional default.
Learn to write readable assertions with Hamcrest matchers in Spring Boot tests, using has size, has items, and every item checks. Explore json, assert, and jsonpath for flexible test verification.
Explore AssertJ for fluent, chainable assertions in unit tests, replacing Hamcrest with has size, contains, all match, none match, and lambda predicates for numbers and strings.
Explore how json path enables asserting json responses in Java tests. Learn to parse service responses, extract IDs, names, quantities, and perform precise assertions with json path.
Explore unit testing patterns with the X Unit Patterns site, learn to test database interactions, external interfaces, and use mocks and spies, while organizing and refactoring test code.
Learn to generate a coverage report in Eclipse and read which lines are executed, emphasizing meaningful asserts over chasing high unit tests coverage figures.
Focus on fast unit tests by keeping scope small, using mocks, and avoiding Spring context when possible to speed up feedback in continuous integration and across layers.
Write good unit tests that are readable, fast, and isolated from external dependencies using mocks or stubs, with clear naming and reusable setup and assert utilities.
Develop a growth mindset to accelerate mastering Java unit testing with Spring Boot and Mockito. Embrace challenges, learn from feedback, celebrate effort, and stay curious for long-term progress.
Explore REST services with Spring Boot and Mockito, writing unit tests for each layer and an integration test, while mastering practical tips for effective unit testing.
Great Programmers write Great Unit Tests. Want to Write Great Unit Tests for Spring Boot REST API ?
Spring Boot Test? Yes. Layered Unit Tests? Yes. Of Course. Spring Boot Integration Tests? Yes.
Mockito? Yes. JsonAssert, Hamcrest Matchers, and JsonPath? Yes. Included.
WHAT OUR LEARNERS ARE SAYING:
5 STARS - This a very comprehensive introduction to spring boot test, which brings Mockito, Junit, JsonAssert, Spring Data JPA, REST, design pattern, and many more. Very valuable course. Thanks.
5 STARS - Excellent and to the point course on Unit testing with good references.
5 STARS - This is an excellent course for both beginners as well as experienced Java developers who which to hone their skill. The instructor was very knowledgeable and knew how to keep the course engaging while providing lots of tips. Highly recommended...
5 STARS - Great Course to learn Junit Testing. Great content and great examples to explain each scenarios of writing test cases. If you want to write java test cases then definitely go through this course.
5 STARS - This course is a great introduction to unit and integration testing using Spring Boot, Mockito, and a number of other libraries that can help to write effective tests. It is easy to follow along with and has plenty of references where to look for more information.
5 STARS - Needed it for new client project. Excellent source of concise tutorials for JUnit of microservices.
5 STARS - I was drowning in unit test concepts and this course cleared up what read about them. And have a simple but clear explanation of when is good and how to create unit test in your app.
COURSE OVERVIEW
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 most popular Java Unit Testing Framework.
In this course, you will learn to Write Great Unit Tests for Simple RESTful Services with Spring Boot Starter Test, Mockito and JUnit. You will learn to write independent unit tests for RESTful web services talking with multiple layers - web, business and data. You will learn how to write integration tests using an in memory database H2.
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 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 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.
COURSE HIGHLIGHTS
Section 1 - Learning Mocking with Mockito
Step 01: Setting up the project using Spring Initializr
Step 02: Writing Unit Test for a Simple Business Service
Step 03: Setting up a Business Service to call a Data Service
Step 04: Writing your first unit test with Stub
Exercise - Update Tests 2 & 3
Step 05: Exercise Solution - Updating Tests 2 & 3 to use Stubs - Problem with Stubs.
Step 06: Writing Unit Tests with Mocking using Mockito
Exercise - Updating Tests 2 & 3 to use Mockito
Step 07: Exercise Solution - Updating Tests 2 & 3 to use Mockito
Step 08: More Refactoring - @Mock, @InjectMocks and @RunWith(MockitoJUnitRunner.class)
Step 09: Mockito Tips - Multiple Return Values and Specific Argument Matchers
Step 10: Mockito Tips - Argument Matchers
Step 11: Mockito Tips - Verify method calls
Step 12: Mockito Tips - Argument Capture
Step 13: Mockito Tips - Argument Capture on Multiple Calls
Step 14: Introduction to Spy
Step 15: Mockito FAQ
Section 2 - Unit Testing RESTful Services with Spring Boot and Mockito
Step 01: Creating a Hello World Controller
Step 02: Using Mock Mvc to test Hello World Controller
Step 03: Using Response Matchers to check status and content
Step 04: Creating a Basic REST Service in Item Controller
Step 05: Unit Testing Item Controller and Basic JSON Assertions
Step 06: Digging deeper into JSON Assert
Step 07: Writing a REST Service talking to Business Layer
Step 08: Writing Unit Test for REST Service mocking Business Layer
Step 09: Prepare Data Layers with JPA, Hibernate and H2
Step 10: Create Item Entity and Populate data with data.sql
Step 11: Create a RESTful Service talking to the database
Step 12: Writing Unit Test for Web Layer - Controller - Using Mock MVC
Step 13: Exercise & Solution - Writing Unit Test for Business Layer - Mocking
Step 14: Writing Unit Test for Data Layer - Data JPA Test
Step 15: Writing an Integration Test using @SpringBootTest
Step 16: Tip : Using @MockBean to mock out dependencies you do not want to talk to!
Step 17: Tip : Creating Different Test Configuration
Step 18: Writing Unit Tests for Other Request Methods
Step 19: Refactor SomeBusinessImpl to use Functional Programming
Step 20: Better Assertions with Hamcrest - HamcrestMatcherTest
Step 21: Better Assertions with AssertJ - AssertJTest
Step 22: Better Assertions with JSONPath - JSONPathTest
Step 23: Easier Static Imports
Step 24: Tip : Measuring Test Coverage with Eclipse
Step 25: Tip : Keep an eye on performance of unit tests!
Step 26: Good Unit Tests
Start Learning Now. Hit the Enroll Button!