Section 18 · Stage 5: Work Like a Developer
Testing, Debugging & Code Quality
Unit tests with JUnit, mocks with Mockito, testing Spring applications, using the debugger and keeping code clean.
What you will be able to do
- Write clear unit tests with JUnit 5 and AssertJ for real classes
- Isolate code under test with Mockito mocks and stubs
- Test Spring controllers, repositories and whole application flows
- Find bugs quickly with the IDE debugger and useful logs
- Measure test coverage and keep code clean with refactoring and static analysis
- Add a dependable test suite to ShopLite
What you will learn
18.1Why Testing Matters7 lessons
What tests protect you from, which kinds of tests to write and how they run in the Maven build.
18.2JUnit 5 in Depth9 lessons
Go past simple assertions to well-organised, data-driven tests.
- Test structure: Arrange, Act, Assert35 min
- Core JUnit assertions40 min
- Lifecycle: @BeforeEach, @AfterEach, @BeforeAll40 min
- Testing exceptions and timeouts40 min
- Parameterised tests with @ValueSource and @CsvSource40 min
- Readable assertions with AssertJ40 min
- Naming and organising tests40 min
- Practice: JUnit 550 min
- Mini project: a test suite for the ShopLite cart75 min
18.3Test-Driven Development7 lessons
Write the test first and let it shape the design.
18.4Mocking with Mockito8 lessons
Test one class at a time by replacing its dependencies with mocks.
- Why mock: slow, external or unfinished dependencies35 min
- @Mock, @InjectMocks and when/thenReturn40 min
- verify and argument captors40 min
- Stubbing exceptions40 min
- Mocks vs stubs vs fakes40 min
- Over-mocking and brittle tests45 min
- Practice: Mockito50 min
- Mini project: unit-test the ShopLite order service60 min
18.5Testing Spring Applications9 lessons
Test each layer of a Spring Boot app with the right test slice.
- Unit testing services without Spring35 min
- @WebMvcTest and MockMvc for controllers40 min
- Validation and error responses in controller tests40 min
- @DataJpaTest for repositories45 min
- @SpringBootTest for full flows45 min
- Testing secured endpoints with mock users45 min
- Test data and test profiles40 min
- Practice: testing Spring applications50 min
- Mini project: test the ShopLite category API60 min
18.6Debugging8 lessons
Find the cause of a bug with the debugger instead of guessing.
- Breakpoints, step over, step into40 min
- Watching variables and evaluating expressions40 min
- Conditional breakpoints40 min
- Reading stack traces from top to bottom40 min
- Debugging a running Spring Boot app45 min
- A step-by-step bug hunting routine40 min
- Practice: debugging50 min
- Mini project: hunt three bugs in ShopLite60 min
18.7Logging for Troubleshooting7 lessons
Write logs that help you find problems in a running application.
18.8Code Quality & Clean Code8 lessons
Keep code easy to read and change as the project grows.
- Naming, small methods and clear intent35 min
- Code smells: duplication, long methods, god classes40 min
- Safe refactoring with tests and IDE tools40 min
- Code coverage with JaCoCo and what it does not tell you35 min
- Static analysis with SonarLint35 min
- Code review checklist30 min
- Practice: clean code45 min
- Mini project: refactor the ShopLite order service60 min
18.9Testing Mini Project: Test ShopLite5 lessons
Add unit, slice and integration tests to ShopLite and fix the bugs they find.
