Unit testing OO code 00:00:00 Make a prediction You will test one observable object collaboration under failure without pinning the test to private helper calls. Checkout receives a decline but its internal helpers all ran in the expected order. Has the test proved that the order stayed unpaid? No. It proved a sequence of calls. Assert the returned decline and the persistent unpaid state, and ensure no paid-only effect occurred. 00:00:27 Asha tests an arm When testing a mechanical arm, give it a known object and inspect where it ends up. Counting every tiny movement of every gear makes an innocent gear redesign look like a failure. Probe the object's public promise and substitute controllable collaborators where necessary. Record an interaction only when the interaction itself matters. A unit test is a small program that builds one object, does one thing with it and checks the outcome. It runs in milliseconds, needs no network, no database and no clock, and gives the same answer every time. Safety. You may change the inside of a class and know in a second whether its promises still hold. 00:01:09 Prove one declined checkout Set up an unpaid order and a fake gateway that always declines. Invoke checkout once. Inspect the resulting order and returned outcome. The order must remain unpaid and the caller must learn that the charge was declined. Avoid asserting that three private helpers ran in a particular order. Those details can change while the promise stays the same. Give the fake gateway a decline, call checkout once, then inspect the order and receipt store. The order stays unpaid and no receipt was issued. If a refactor changes three private calls into one, these assertions continue to test what a customer can observe. 00:01:50 Test a time boundary deterministically A reservation expires at noon. Supply a clock that reads one instant before noon and confirm the hold is valid. Move the same clock to noon and run the check again. A sleep-based test obscures the boundary and slows every run. Run the eligibility check at eleven fifty-nine and fifty-nine seconds, then set the same clock to noon. An explicit comparison at the boundary gives a repeatable result. Sleeping for one real second can wake late and leave the bug undetected. A fake gateway can drift from the real provider. Passing a unit test does not validate every external assumption. Add a small integration or contract test for the provider boundary while keeping most state and edge-case tests fast and deterministic. Python’s standard mocking library can build a test double constrained to the signatures of a real object. Mockito supports configuring responses and checking interactions in Java. Use those capabilities when the interaction is part of the contract. For an ordinary refactor, asserting the resulting receipt is often more durable than asserting a private call order. 00:03:02 Try an interview follow-up Would you make test driven development a rule? No, I would make the result a rule: new behaviour comes with a test that fails without it, reviewed as code. How people get there may differ. I would teach seams and contract tests, because lack of those is what makes teams give up. A payment service calls three private helpers before recording a receipt. A refactor merges two helpers. A test that checks the receipt amount and identifier can keep passing. Also force the payment gateway to decline and check that no paid receipt appears. These checks protect behavior across the refactor. 00:03:45 What to take with you Feed a class its collaborators, replace the slow and random ones by doubles, and check one promise per test in three parts: arrange, act, assert. Prefer real objects, then fakes, then stubs. Check interactions only where the call itself is the promise, such as one mail sent or no charge made.