Skip to main content

MODULE 9 - Testing with Spring Boot

Welcome to Module 9! πŸ§ͺ​

Your application works... but how can you be sure it will always work? After every change, after every deployment? Automated tests are your safety net.

This module teaches you how to write professional tests that ensure the quality of your code.


πŸŽ“ Learning Objectives​

By the end of this module, you will be able to:

  1. βœ… Understand the testing pyramid
  2. βœ… Write unit tests with JUnit 5
  3. βœ… Use Mockito to mock dependencies
  4. βœ… Create integration tests with @SpringBootTest
  5. βœ… Test REST APIs with MockMvc
  6. βœ… Measure code coverage

πŸ“š Prerequisites​

Before starting this module, you should have:

  • βœ… Completed Modules 1 to 8
  • βœ… Mastered the 3-layer architecture
  • βœ… Created Spring services and repositories
  • βœ… Be comfortable with REST APIs

πŸ—ΊοΈ Module Outline​

Theoretical Part (5 chapters)​

Chapter 1: Introduction to Testing​

Duration: 30 minutes

Content:

  • Why test?
  • The testing pyramid
  • Unit vs integration vs E2E tests
  • TDD and BDD
  • Maven/Gradle setup

Why important? Understand the testing strategy


Chapter 2: Unit Testing with JUnit 5​

Duration: 1 hour

Content:

  • JUnit 5 annotations (@Test, @BeforeEach, etc.)
  • Assertions (assertEquals, assertTrue, assertThrows)
  • Parameterized tests
  • Test lifecycle
  • Best practices

Why important? The foundation of all tests


Chapter 3: Mocking with Mockito​

Duration: 1 hour

Content:

  • Why mock?
  • @Mock, @InjectMocks, @Spy
  • when().thenReturn()
  • verify() and argument matchers
  • Stubbing and exceptions

Why important? Isolate the units to test


Chapter 4: Integration Testing​

Duration: 45 minutes

Content:

  • @SpringBootTest
  • @DataJpaTest for repositories
  • @WebMvcTest for controllers
  • TestContainers
  • Test profiles

Why important? Test components together


Chapter 5: REST API Testing​

Duration: 45 minutes

Content:

  • MockMvc in detail
  • WebTestClient
  • Testing JSON responses
  • Testing errors and exceptions
  • RestAssured (bonus)

Why important? Validate your endpoints


Practical Part (3 Labs)​

Lab 9.1: Unit Tests - Service Layer​

Duration: 1h30

Objective: Test a complete service layer

You will:

  • Write tests for a ProductService
  • Mock the repository
  • Test nominal and error cases
  • Achieve 80%+ coverage

Lab 9.2: Integration Tests - Repository​

Duration: 1h30

Objective: Test persistence with a real DB

You will:

  • Use @DataJpaTest
  • Test custom queries
  • Verify constraints
  • Use H2 for tests

Lab 9.3: REST API Tests - Controller​

Duration: 1h30

Objective: Test the complete REST API

You will:

  • Use MockMvc
  • Test all CRUD endpoints
  • Validate JSON responses
  • Test security

πŸ“Š Total Duration​

TypeDuration
Theory4h (5 chapters)
Practice4h30 (3 Labs)
Total8h30 (~2-3 sessions)

🎯 What You Will Understand​

Before this Module​

// You test manually with Postman... 😰
// And after every change:
// "Does it still work?"
// "I'm afraid of breaking something..."
// "I don't dare to refactor..."

After this Module​

@Test
void shouldCalculateOrderTotal() {
// Given
Order order = new Order();
order.addItem(new Item("Laptop", 1000.0, 2));
order.addItem(new Item("Mouse", 50.0, 1));

// When
double total = orderService.calculateTotal(order);

// Then
assertThat(total).isEqualTo(2050.0);
}

// βœ… 150 tests passed in 3.2s
// Coverage: 87%
// Confidence: 100% 😎

You will know how to:

  • βœ… Write readable and maintainable tests
  • βœ… Mock dependencies correctly
  • βœ… Test each layer of the application
  • βœ… Refactor without fear
  • βœ… Automatically detect regressions

πŸš€ Why This Module is Essential​

1. Confidence in Code​

Without tests:
- "I think it works" 🀞
- Fear of changing existing code
- Bugs discovered in production

With tests:
- "I know it works" βœ“
- Confident refactoring
- Bugs detected immediately

2. Living Documentation​

@Test
void userShouldNotBeAbleToWithdrawMoreThanBalance() {
// This test documents a business rule!
Account account = new Account(100.0);

assertThrows(InsufficientFundsException.class,
() -> account.withdraw(150.0));
}

3. Required Skill​

Java job offers:
- "TDD/BDD" βœ“
- "JUnit, Mockito" βœ“
- "Code coverage > 80%" βœ“
- "Automated tests mandatory" βœ“

πŸ—ΊοΈ Progress in the Training​

Module 9 = Ensuring quality before deployment!


πŸ“– How to Approach This Module​

Theory AND Practice Together​

  1. Chapter 1 (Introduction) β†’ Understand the strategy
  2. Chapter 2 (JUnit) + Lab 9.1 β†’ Practice immediately
  3. Chapter 3 (Mockito) β†’ Complete Lab 9.1 with mocks
  4. Chapter 4 (Integration) + Lab 9.2 β†’ Repository tests
  5. Chapter 5 (API) + Lab 9.3 β†’ Controller tests

Intensive Option (1.5 days):

  • Day 1 Morning: Chapters 1-2 + start Lab 9.1
  • Day 1 Afternoon: Chapter 3 + finish Lab 9.1
  • Day 2 Morning: Chapters 4-5 + Lab 9.2
  • Day 2 Afternoon: Lab 9.3

Progressive Option (3 sessions):

  • Session 1: Chapters 1-2 + Lab 9.1
  • Session 2: Chapters 3-4 + Lab 9.2
  • Session 3: Chapter 5 + Lab 9.3

βœ… Self-Assessment​

Before starting, answer:

  1. Do you know what a unit test is? ☐ Yes ☐ No
  2. Have you ever used JUnit? ☐ Yes ☐ No
  3. Do you know about mocking? ☐ Yes ☐ No
  4. Have you heard of MockMvc? ☐ Yes ☐ No
  5. Do you know what code coverage is? ☐ Yes ☐ No

After the module, you should answer "Yes" to EVERYTHING!


🎯 Next Step​

Ready to write professional tests?

➑️ Start with Chapter 1: Introduction to Testing


⬅️ Back to Course Outline