Testing Java Applications with JUnit 5 and Mockito
Learn JUnit 5 and Mockito by adding tests to a pre-built e-commerce application. Covers unit tests, mocking, MockMvc, @DataJpaTest, TDD, and integration tests with Testcontainers.
View badge details
About This Course
Course Curriculum
14 Lessons
Unit Testing Fundamentals and JUnit 5
Learn why testing matters, the test pyramid, JUnit 5 architecture, @Test, @BeforeEach/@AfterEach, Assertions, @DisplayName, @Nested, @ParameterizedTest with @ValueSource and @CsvSource, and test naming conventions.
Unit Testing - Lab Exercises
Hands-on practice writing JUnit 5 unit tests for Order, OrderItem, and Product domain classes in the E-Commerce Order Processing System.
Mockito - Mocking Dependencies
Learn why mocking exists, Mockito library, @Mock, @InjectMocks, when/thenReturn, verify, ArgumentCaptor, @Spy, doReturn for void methods, matchers, and strict stubbing.
Mockito - Lab Exercises
Hands-on practice writing Mockito tests for OrderService with mocked OrderRepository and ProductService, using @Mock, @InjectMocks, when/thenReturn, verify, and ArgumentCaptor.
Testing Spring Boot Controllers
Learn how to test Spring Boot REST controllers using @WebMvcTest and MockMvc without starting a real server. Covers @MockBean, perform/andExpect, jsonPath assertions, and testing validation errors and 404 responses.
Testing Spring Boot Controllers - Lab Exercises
Practice writing @WebMvcTest tests for OrderController and ProductController using MockMvc, @MockBean, and jsonPath assertions.
Testing the Repository Layer
Learn how to test Spring Data JPA repositories using @DataJpaTest, TestEntityManager, and H2 in-memory database. Covers derived queries, @Query JPQL methods, Pageable, and @Transactional auto-rollback.
Repository Layer Testing - Lab Exercises
Practice writing @DataJpaTest tests for OrderRepository and ProductRepository using TestEntityManager to persist test data and verify query methods.
Test-Driven Development (TDD)
Learn the Red-Green-Refactor TDD cycle: writing failing tests first, implementing the minimum code to make them pass, then refactoring. Covers triangulation, @Disabled, and when TDD is and is not the right approach.
Test-Driven Development - Lab Exercises
Practice the TDD Red-Green-Refactor cycle by building DiscountService test-first: write failing tests, implement minimum code to pass, then refactor.
Integration Testing with @SpringBootTest
Learn how to write full-stack integration tests using @SpringBootTest, TestRestTemplate, and Testcontainers for real PostgreSQL. Covers WebEnvironment options, @Transactional rollback in tests, @ActiveProfiles, and @Sql fixtures.
Integration Testing - Lab Exercises
Practice writing @SpringBootTest integration tests using TestRestTemplate and Testcontainers PostgreSQL to test the full order lifecycle end-to-end.
Capstone Briefing: Building a Complete Test Suite
Review all test types from the course and receive your capstone assignment: write the complete test suite for the ReturnOrder feature (ReturnOrderService, ReturnOrderController, ReturnOrderRepository) across all five test layers.
Capstone Project: Testing the ReturnOrder Feature
Write the complete test suite for the ReturnOrder feature covering all test layers: unit tests, Mockito service tests, @WebMvcTest controller tests, @DataJpaTest repository tests, and @SpringBootTest integration tests.