AI Instructor Live Labs Included

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.

Intermediate
17h 31m
14 Lessons
SMU-JAVA-TEST
Java Testing with JUnit and Mockito Badge

View badge details

About This Course

Build comprehensive test coverage for Java applications using JUnit 5, Mockito, and Spring Boot's testing infrastructure. This course takes a pre-built, untested E-Commerce Order Processing System and adds tests layer by layer: unit tests, Mockito mocks, MockMvc controller tests, repository tests with @DataJpaTest, TDD with a new feature, and full integration tests with Testcontainers. By the capstone you will have a complete, professional-grade test suite.

Course Curriculum

14 Lessons
01
AI Lesson
AI Lesson

Unit Testing Fundamentals and JUnit 5

1h 0m

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.

02
Lab Exercise
Lab Exercise

Unit Testing - Lab Exercises

1h 45m 4 Exercises

Hands-on practice writing JUnit 5 unit tests for Order, OrderItem, and Product domain classes in the E-Commerce Order Processing System.

Write Order Domain Unit Tests ~15 min
Test Order.addItem with Multi-Item Total Calculation ~15 min
Write Parameterized Tests for Product.isInStock ~15 min
Run the Full Test Suite with mvn test ~15 min
03
AI Lesson
AI Lesson

Mockito - Mocking Dependencies

1h 0m

Learn why mocking exists, Mockito library, @Mock, @InjectMocks, when/thenReturn, verify, ArgumentCaptor, @Spy, doReturn for void methods, matchers, and strict stubbing.

04
Lab Exercise
Lab Exercise

Mockito - Lab Exercises

1h 33m 4 Exercises

Hands-on practice writing Mockito tests for OrderService with mocked OrderRepository and ProductService, using @Mock, @InjectMocks, when/thenReturn, verify, and ArgumentCaptor.

Set Up OrderServiceTest with Mockito Annotations ~12 min
Write createOrder_success with Stubbing and Verify ~12 min
Test Insufficient Stock with verify(never()) ~12 min
Capture Saved Order with ArgumentCaptor ~12 min
05
AI Lesson
AI Lesson

Testing Spring Boot Controllers

45m

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.

06
Lab Exercise
Lab Exercise

Testing Spring Boot Controllers - Lab Exercises

1h 33m 4 Exercises

Practice writing @WebMvcTest tests for OrderController and ProductController using MockMvc, @MockBean, and jsonPath assertions.

Set Up OrderControllerTest with @WebMvcTest ~12 min
Test GET Order Returns 404 When Not Found ~12 min
Test POST Order Returns 201 Created ~12 min
Write ProductController Tests ~12 min
07
AI Lesson
AI Lesson

Testing the Repository Layer

45m

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.

08
Lab Exercise
Lab Exercise

Repository Layer Testing - Lab Exercises

1h 33m 4 Exercises

Practice writing @DataJpaTest tests for OrderRepository and ProductRepository using TestEntityManager to persist test data and verify query methods.

Set Up OrderRepositoryTest with @DataJpaTest ~12 min
Test findByStatus with Pagination ~12 min
Write ProductRepository Low-Stock Query Test ~12 min
Test findTopSellingProducts Native Query ~12 min
09
AI Lesson
AI Lesson

Test-Driven Development (TDD)

45m

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.

10
Lab Exercise
Lab Exercise

Test-Driven Development - Lab Exercises

1h 33m 4 Exercises

Practice the TDD Red-Green-Refactor cycle by building DiscountService test-first: write failing tests, implement minimum code to pass, then refactor.

RED Phase: Write the First Failing Test ~12 min
GREEN Phase: Implement the Threshold Check ~12 min
Triangulate: PREMIUM Tier and Refactor ~12 min
Boundary Tests and Full Suite Verification ~12 min
11
AI Lesson
AI Lesson

Integration Testing with @SpringBootTest

45m

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.

12
Lab Exercise
Lab Exercise

Integration Testing - Lab Exercises

1h 34m 4 Exercises

Practice writing @SpringBootTest integration tests using TestRestTemplate and Testcontainers PostgreSQL to test the full order lifecycle end-to-end.

Set Up OrderIntegrationTest with @SpringBootTest ~12 min
Test Full Order Creation with Items ~12 min
Add Testcontainers PostgreSQL ~15 min
Test Invalid Product Returns 404 ~10 min
13
AI Lesson
AI Lesson

Capstone Briefing: Building a Complete Test Suite

30m

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.

14
Lab Exercise
Lab Exercise

Capstone Project: Testing the ReturnOrder Feature

2h 30m 6 Exercises

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.

Unit Tests for ReturnOrder Domain Logic ~15 min
Mockito Tests for ReturnOrderService ~20 min
@WebMvcTest for ReturnOrderController ~20 min
@DataJpaTest for ReturnOrderRepository ~15 min
@SpringBootTest Integration Test for Return Lifecycle ~20 min
Stretch Goal: Code Coverage with JaCoCo ~15 min

This course includes:

  • 24/7 AI Instructor Support
  • Live Lab Environments
  • 7 Hands-on Lessons
  • 6 Months Access
  • Completion Badge
  • Certificate of Completion
Java Testing with JUnit and Mockito Badge

Earn Your Badge

Complete all lessons to unlock the Java Testing with JUnit and Mockito achievement badge.

Category
Skill Level Intermediate
Total Duration 17h 31m
Java Testing with JUnit and Mockito Badge
Achievement Badge

Java Testing with JUnit and Mockito

Awarded for completing the Java Testing course, demonstrating proficiency in unit testing, integration testing, mocking, and TDD with JUnit 5 and Mockito.

Course Testing Java Applications with JUnit 5 and Mockito
Criteria Complete all lessons and pass assessments in the Java Testing with JUnit and Mockito course.
Valid For 730 days

Skills You'll Earn

JUnit 5 Mockito Test-Driven Development Integration Testing Spring Boot Test Test Coverage

Complete all lessons in this course to earn this badge