Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level Three · Lesson 8 of 9

Module Seven

📖 Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
8
Module 7: Testing, Refactoring & Code Quality for Young Coders
⭐ Module 7 · Level Three

Testing, Refactoring
& Code Quality

🧪 Prove your code works — and keep it clean forever

🎯 What you'll learn in this module

  • ✔ How to write unit, integration, and property‑based tests
  • ✔ The four test doubles: mocks, stubs, fakes, and spies
  • ✔ How to use Test‑Driven Development (TDD) to design better code
  • ✔ The refactoring catalog: extract class, inline, move method
  • ✔ How static analysis, linters, and metrics keep quality high
  • ✔ How continuous integration catches bugs before users do
🧪
Unit, Integration & Property‑Based Tests
Testing

A unit test checks one small piece of code — like a single method — in isolation. Fast and precise. An integration test checks that multiple pieces work together, like a database plus a service plus a controller.

Property‑based tests are the cool ones: instead of checking one specific input, you describe properties that should always hold ("reversing a list twice gives back the original"). The tool generates hundreds of random inputs to try to break it.

🧱 Unit test = check one LEGO brick. Integration test = check that five bricks click together. Property test = shake the whole build from every angle to see if it falls apart.
🎭
Test Doubles: Mocks, Stubs, Fakes & Spies
Testing

Sometimes you can't use the real thing in a test — maybe it's slow (a database) or unpredictable (a weather API). That's when you use a test double, a stand‑in object:

Stub — returns a canned answer. Mock — verifies that specific calls were made. Fake — a simpler working version (like an in‑memory database). Spy — records calls so you can inspect them after the test.

🎬 Test doubles are stunt doubles for actors: they look similar enough to fool the scene, but they're safer and easier to control.
🔁
Test‑Driven Development: Red, Green, Refactor
Method

TDD flips the usual order: you write the test before the code. The cycle has three steps:

🔴 Red — write a failing test
→
🟢 Green — write minimal code to pass
→
🔵 Refactor — clean up while tests stay green

The result: code that's exactly as complex as needed, backed by tests that prove it works. It feels slow at first but saves hours of debugging later.

🎯 Like drawing a target first, then shooting arrows. Instead of guessing where to aim, you know exactly when you've hit the bullseye.
📚
Refactoring: The Catalog of Fixes
Refactoring

Refactoring means improving code structure without changing its behaviour. It's a bit like tidying your room: nothing new arrives, but now you can find everything. Common moves include:

Extract Class — split one big class into two focused ones. Inline — merge a tiny class back into its caller. Move Method — relocate a method to the class that uses it most. Rename — make unclear names obvious.

🧹 A messy desk still "works" — but organizing it makes you faster and less stressed.
🔍
Static Analysis, Linters & Metrics
Quality

Static analysis tools scan your code without running it, looking for bugs, bad patterns, and risky code. Linters check style and formatting (e.g., flake8, ESLint). Code metrics measure things like complexity, duplication, and test coverage.

You'll learn to read the warnings — not ignore them — and set thresholds so your code can't slowly rot into a mess.

🩺 Static analysis is a health check for your code: it spots problems before they hurt anyone.
🔄
Continuous Integration: The Safety Net
Quality

Continuous Integration (CI) runs your tests automatically every time you push code. If something breaks, you know within minutes. No more "it worked on my laptop."

Popular services like GitHub Actions or GitLab CI can also run linters, static analysis, and deployment steps. Combined with code review, CI is the safety net that catches bugs before they reach users.

🕵️ CI is a security guard for every change: no code gets in without passing the checks.
⚠️ The real goal: Tests and refactoring aren't about perfection — they're about confidence. When you have solid tests, you can refactor fearlessly. When you refactor, tests stay honest. Together they let you keep improving without breaking things.

🛠️ Hands-on activities (no boring stuff!)

1

First unit test: Write a Calculator class with add() and divide(). Write tests that cover happy paths (2 + 2 = 4) and edge cases (divide by zero). Run them.

2

Stub a slow service: Create a WeatherService stub that always returns 22 °C. Use it in a test to verify that your OutfitRecommender suggests t‑shirt weather.

3

TDD kata: Using TDD, build a FizzBuzz function. Write the test first (fails), implement minimum code to pass, then refactor. Do this for each rule.

4

Extract class: Take a User class that handles account data, email formatting, and database saving. Refactor by extracting UserRepository and EmailFormatter classes.

5

Lint run: Install a linter for your language (flake8 for Python, ESLint for JavaScript). Run it on a recent project. Fix the top five warnings.

6

CI setup: Push a small project to GitHub and add a GitHub Actions workflow that runs tests on every push. Break a test on purpose and watch CI catch it.

🏆

By the end of this module, you'll be able to: write unit and integration tests, use mocks and stubs, follow the TDD cycle, apply common refactorings, run linters and static analysis, and set up CI to keep your projects healthy.

🏆 Get Certified

🔒

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

🎓 Sign Up to Get Certified
🛠️ Practice Tools
Hands-on simulators & labs - subscription required.
→
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
→