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:
- β Understand the testing pyramid
- β Write unit tests with JUnit 5
- β Use Mockito to mock dependencies
- β Create integration tests with @SpringBootTest
- β Test REST APIs with MockMvc
- β 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β
| Type | Duration |
|---|---|
| Theory | 4h (5 chapters) |
| Practice | 4h30 (3 Labs) |
| Total | 8h30 (~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β
- Chapter 1 (Introduction) β Understand the strategy
- Chapter 2 (JUnit) + Lab 9.1 β Practice immediately
- Chapter 3 (Mockito) β Complete Lab 9.1 with mocks
- Chapter 4 (Integration) + Lab 9.2 β Repository tests
- Chapter 5 (API) + Lab 9.3 β Controller tests
Recommended Timeβ
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:
- Do you know what a unit test is? β Yes β No
- Have you ever used JUnit? β Yes β No
- Do you know about mocking? β Yes β No
- Have you heard of MockMvc? β Yes β No
- 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