Latest / CyberSecurity Summary / Effective Unit Testing: A guide for Java developers
Transcript
- 0:00Speaker 1: You know, usually when we talk about a diagnosis, there's
- 0:03Speaker 1: this this expectation of precision.
- 0:06Speaker 2: Right like engineering.
- 0:07Speaker 1: Exactly like you break your arm, the x ray shows
- 0:10Speaker 1: that jagged white line and the doctor just points and says,
- 0:12Speaker 1: there it is broken or not broken. It's clean.
- 0:15Speaker 2: Yeah, it's comforting. We really like things to be visible
- 0:18Speaker 2: and neatly categorized.
- 0:19Speaker 1: We do. But then you step into the world of
- 0:22Speaker 1: complex software systems and suddenly that X ray machine is
- 0:26Speaker 1: just broken. We're looking at a diagnostic landscape that is well, honestly,
- 0:30Speaker 1: it's murky.
- 0:31Speaker 2: It is the absolute definition of diagnostic muddy waters. I mean,
- 0:35Speaker 2: when a massive application fails, there is no simple jagged
- 0:39Speaker 2: white line to point out no, and.
- 0:41Speaker 1: That is exactly what we are fixing. In today's deep dive,
- 0:44Speaker 1: we are extracting the core insights from the book Effective
- 0:47Speaker 1: Unit Testing, a Guide for Java Developers by Lasa Cascala.
- 0:51Speaker 2: A fantastic resource, truly.
- 0:53Speaker 1: And you know, whether you write code every day, manage
- 0:55Speaker 1: technical projects, or just want to know why the digital
- 0:57Speaker 1: infrastructure you rely on occasionally just falls apart. Our mission
- 1:01Speaker 1: today is to show you how verifying truth and a
- 1:03Speaker 1: system completely changes how you design it.
- 1:05Speaker 2: Because It's not just a manual on catching bugs.
- 1:07Speaker 1: Right right, We're going to unpack the philosophy of how
- 1:10Speaker 1: writing good tests boosts daily productivity and frankly keeps a
- 1:15Speaker 1: code base from turning into an unreadable nightmare.
- 1:18Speaker 2: And to the listener, even if you don't write a
- 1:20Speaker 2: single line of Java or any code for that matter,
- 1:23Speaker 2: pay close attention.
- 1:24Speaker 1: Yeah, don't tune out.
- 1:26Speaker 2: Exactly, because the concepts we are going to explore, things
- 1:30Speaker 2: like avoiding happy tests and using test doubles, they apply
- 1:34Speaker 2: profoundly to problem solving and system design in general. It
- 1:39Speaker 2: is really about how we build resilient workflows in well,
- 1:42Speaker 2: any complex environment.
- 1:43Speaker 1: Okay, let's unpack this. Before we get into the nuts
- 1:46Speaker 1: and bolts of how to write a good test, we
- 1:49Speaker 1: have to tackle the fundamental why, like why do we
- 1:51Speaker 1: test at all?
- 1:52Speaker 2: That's the core question.
- 1:53Speaker 1: Because I think the default assumption, especially for management, is
- 1:56Speaker 1: that the goal is one hundred percent code coverage, the
- 1:59Speaker 1: logic being you know, if every line of code is tested,
- 2:01Speaker 1: the software is perfect.
- 2:02Speaker 2: Yeah, and that assumption is a very common and very
- 2:05Speaker 2: dangerous trap. The source material shares a great anecdote to illustrate.
- 2:09Speaker 1: So the Marcus story, Yes, Marcus.
- 2:11Speaker 2: So there's this young programming whiz named Marcus who joins
- 2:15Speaker 2: an investment bank. He's tasked with writing unit tests for
- 2:18Speaker 2: their self service web application, and over time, he gets
- 2:22Speaker 2: the team's test coverage metric up to an impressive ninety
- 2:25Speaker 2: eight percent.
- 2:26Speaker 1: Which I mean that looks fantastic on a manager's dashboard.
- 2:29Speaker 1: You'd think he would just push for that last two percent,
- 2:31Speaker 1: get the perfect score, and you know, pop the champagne.
- 2:34Speaker 2: You'd think he considers it. But then Marcus realizes that
- 2:38Speaker 2: pushing for one hundred percent is actually a complete waste
- 2:41Speaker 2: of time. Wait why because the remaining two percent of
- 2:44Speaker 2: the code consisted of what are called empty method stubs.
- 2:47Speaker 1: Okay, let's translate that for the non coders listening. What
- 2:50Speaker 1: exactly is an empty method stub?
- 2:52Speaker 2: Well, imagine a contractor who installs a doorframe but no
- 2:56Speaker 2: actual door.
- 2:56Speaker 1: Okay, I can picture that, right.
- 2:58Speaker 2: So if a building's comes by with a checklist and
- 3:01Speaker 2: asks is there a frame here? The answer is yes,
- 3:05Speaker 2: check one hundred percent coverage. But if a burglar walks
- 3:08Speaker 2: right through the empty frame, you have a massive security flaw.
- 3:12Speaker 1: Ah, I see.
- 3:13Speaker 2: In code, an empty method stub is essentially a placeholder
- 3:18Speaker 2: required by the architecture, but it doesn't actually perform any
- 3:21Speaker 2: logic testing it yield zero value.
- 3:24Speaker 1: So Marcus realized that one hundred percent coverage doesn't guarantee
- 3:28Speaker 1: a lack of defects.
- 3:29Speaker 2: Exactly. It literally just guarantees that the coin was executed
- 3:32Speaker 2: during the test. It does not mean the code is
- 3:35Speaker 2: doing what it's actually supposed to do.
- 3:36Speaker 1: Wow. So a test could run perfectly touch every single
- 3:40Speaker 1: line of code, and the software could still be entirely
- 3:42Speaker 1: broken from a user's perspective, exactly. That completely shifts how
- 3:46Speaker 1: you have to look at.
- 3:47Speaker 2: Those metrics it has to and the financial cost of
- 3:50Speaker 2: missing those defects because you were chasing a vanity metric.
- 3:53Speaker 2: It's staggerant Google data, right, Yeah. The text references data
- 3:57Speaker 2: presented by Mark Streebeck at a Google conference. They track
- 4:01Speaker 2: the cost of fixing a bug based entirely on what
- 4:04Speaker 2: it was found in the development life cycle.
- 4:05Speaker 1: So how much does it change?
- 4:07Speaker 2: Well, if a programmer catches and fixes a bug immediately
- 4:10Speaker 2: while they are writing the code, it costs about five
- 4:13Speaker 2: dollars in terms of time and effort.
- 4:15Speaker 1: Five bucks. I mean that's just hitting backspace and retyping
- 4:18Speaker 1: a few lines, right, But if.
- 4:20Speaker 2: That bug escapes the programmer and is found during a
- 4:23Speaker 2: full build of the project, the cost jumps to fifty.
- 4:26Speaker 1: Dollars okay, ten times more, and.
- 4:28Speaker 2: It gets worse. If it makes it into integration testing
- 4:32Speaker 2: where different parts of the software are combined, it's five
- 4:35Speaker 2: hundred dollars.
- 4:36Speaker 1: Oh wow.
- 4:37Speaker 2: If it slips all the way to a system test
- 4:39Speaker 2: right before release, it costs five thousand dollars to fix.
- 4:42Speaker 1: Five dollars to five thousand dollars just based on when
- 4:45Speaker 1: you catch it. I'm guessing that exponential explosion isn't just
- 4:48Speaker 1: because the bug got bigger.
- 4:50Speaker 2: No, not at all. It's about context switching and compounding complexity.
- 4:54Speaker 2: If you find the bug immediately, the mental model of
- 4:57Speaker 2: the code is still fresh in your mind.
- 4:59Speaker 1: Right, you're already in the zone exactly.
- 5:01Speaker 2: But if it's found weeks later in a system test,
- 5:04Speaker 2: the developer has to stop their current project, dig back
- 5:07Speaker 2: into old code they've completely forgotten, figure out how that
- 5:11Speaker 2: bug corrupted other interconnected systems, and essentially untangle a massive knot.
- 5:16Speaker 1: It's like trying to fix a leaky pipe before the
- 5:18Speaker 1: walls are put up, versus having to tear down the
- 5:20Speaker 1: drywall replace the flooring, and repaint the entire living room
- 5:24Speaker 1: weeks later.
- 5:25Speaker 2: That is the exact mechanism driving the cost. And this
- 5:28Speaker 2: dynamic introduces what the author calls the law of the
- 5:31Speaker 2: two Plateaus, which tracks the evolution of a programmer's mindset.
- 5:35Speaker 1: Okay, so what's the first plateau?
- 5:37Speaker 2: The first plateau is where Marcus started outseeing tests simply
- 5:41Speaker 2: as a quality assurance tool, a way to verify that
- 5:44Speaker 2: the code works and catch regressions. You write tests, you
- 5:47Speaker 2: get diminishing returns around that ninety percent mark, and you plateau.
- 5:51Speaker 1: Because you've extracted all the value you can out of
- 5:54Speaker 1: mirror verifications. So how do you break through to the
- 5:56Speaker 1: second plateau?
- 5:57Speaker 2: You have to completely change your mindset. You stop using
- 6:00Speaker 2: tests just for verification and you start using them as
- 6:03Speaker 2: a design tool.
- 6:04Speaker 1: A design tool.
- 6:05Speaker 2: Yeah, the book introduces Marcus's mentor, a senior architect named Sebastian,
- 6:09Speaker 2: who worked entirely differently. Instead of writing code and then
- 6:12Speaker 2: writing a test to check it, Sebastian wrote a failing test.
- 6:15Speaker 1: First, flipping the order like that, writing the test before
- 6:18Speaker 1: the code even exists feels incredibly counterintuitive. Is that a
- 6:23Speaker 1: standard practice or was he just a robe genius.
- 6:26Speaker 2: It is a profound methodology known as test driven development
- 6:30Speaker 2: or PDD. Sebastian would define the exact behavior he wanted
- 6:35Speaker 2: the code to achieve in a test, watch it fail
- 6:38Speaker 2: because the code to execute it didn't exist yet, and
- 6:40Speaker 2: then write just enough simple code to make that test pass.
- 6:43Speaker 1: It sounds like building the mold before you pour the concrete,
- 6:47Speaker 1: rather than just poking the concrete after it dries to
- 6:49Speaker 1: see if it happens to be the right shape.
- 6:51Speaker 2: I love that analogy. By building the mold first, you
- 6:54Speaker 2: only pour exactly as much concrete as you need. This
- 6:57Speaker 2: keeps the code incredibly simple and avoids what developers call
- 7:00Speaker 2: accidental complexity or gold plating.
- 7:02Speaker 1: Gold plating like making it too fancy.
- 7:04Speaker 2: Basically, yeah, it's when you add a bunch of unnecessary
- 7:07Speaker 2: features that no one actually asked for, which only serves
- 7:09Speaker 2: to introduce more places for those five thousand dollars bugs
- 7:12Speaker 2: to hide. The traditional cycle is design code Test, TDD
- 7:17Speaker 2: flips that to test code refactor.
- 7:20Speaker 1: The text also touches on behavior driven development or BDB.
- 7:24Speaker 1: Is that just a rebranding of TDD.
- 7:25Speaker 2: It's an evolution champion by a consultant named Dan North.
- 7:29Speaker 2: He realized that the very word test was confusing people.
- 7:33Speaker 2: It made managers and developers think only about quality assurance
- 7:36Speaker 2: and catching errors.
- 7:37Speaker 1: Oh so they were getting stuck on the word itself.
- 7:39Speaker 2: Exactly, so he shifted the vocabulary. Instead of saying, write
- 7:42Speaker 2: a test, he said, describe the behavior. It forces you
- 7:46Speaker 2: to think about what the system should do from a
- 7:49Speaker 2: user's perspective, rather than just how the code functions mechanically.
- 7:53Speaker 1: Okay, so using tests as a design blueprint is clearly powerful.
- 7:57Speaker 1: But a blueprint is completely useless if the next architect
- 8:00Speaker 1: who looks at it can't decipher your handwriting. Does the
- 8:03Speaker 1: book address what happens when these test files become too
- 8:06Speaker 1: complex to read?
- 8:07Speaker 2: Oh, it makes a very strong case. The test readability
- 8:10Speaker 2: is the exact same thing as code maintainability. Tests are code.
- 8:14Speaker 2: If your test code is an unreadable mess, it becomes
- 8:17Speaker 2: a massive cognitive burden for the team. The text highlights
- 8:21Speaker 2: this with a brilliant anti pattern the JS one K competition.
- 8:25Speaker 1: Oh this was wild. Explain what these hackers are doing?
- 8:28Speaker 2: In this competition, developers try to squeeze complex three D
- 8:32Speaker 2: animations into a JavaScript file that is a maximum of
- 8:36Speaker 2: one twenty four bytes.
- 8:37Speaker 1: That is tiny, it really is.
- 8:39Speaker 2: To make it fit, they use insane compression tricks. They
- 8:43Speaker 2: rename variables to single random letters, They strip out all
- 8:47Speaker 2: formatting spaces and line breaks.
- 8:49Speaker 1: And the result is what the author hilariously calls write
- 8:52Speaker 1: only code.
- 8:53Speaker 2: Yes, because you can write it in the moment, but
- 8:56Speaker 2: if you look at it a week later, it is
- 8:58Speaker 2: just an undecipherable wall of symbols. You could never maintain
- 9:01Speaker 2: it or.
- 9:02Speaker 1: Update it, right, It's just jibberish at that point.
- 9:04Speaker 2: Now, workplace code usually isn't that extreme, but studies show
- 9:07Speaker 2: a direct, strong correlation between poor readability and high defect density.
- 9:13Speaker 2: When a human brain has to expend massive amounts of
- 9:15Speaker 2: cognitive energy just to figure out what a block of
- 9:18Speaker 2: code is trying to do, mistakes happen, And if.
- 9:20Speaker 1: You're a manager onboarding a new developer, you're paying them
- 9:23Speaker 1: to sit there and decipher hieroglyphics instead of actually building
- 9:26Speaker 1: the product exactly. The author points out this is especially
- 9:29Speaker 1: toxic when it comes to automated tests, specifically calling out
- 9:33Speaker 1: the nightmare of the thirty minute monolithic test.
- 9:36Speaker 2: Yes, imagine you have a massive application and instead of
- 9:41Speaker 2: many small, targeted tests, you have one giant thirty minute
- 9:45Speaker 2: script that runs through everything at once. You make a
- 9:48Speaker 2: tiny change to how mailing addresses are formatted, and twenty
- 9:51Speaker 2: five minutes into the test, it fails.
- 9:54Speaker 1: So you have to dig through this unstructured monolith to
- 9:57Speaker 1: find exactly where your tiny change broke things. And then
- 10:00Speaker 1: you have to run the whole thirty minute test again
- 10:02Speaker 1: just to see if your fixed.
- 10:03Speaker 2: Work, which completely destroys the fast feedback loop we established earlier.
- 10:07Speaker 1: Wait, if a test is just too long, can I
- 10:09Speaker 1: just you know, chop it up into one hundred tiny,
- 10:11Speaker 1: separate files. Wouldn't that solve the monolith problem?
- 10:14Speaker 2: Arbitrary structure is honestly just as bad as no structure.
- 10:17Speaker 2: If you just chop a monolith up randomly, you aren't
- 10:19Speaker 2: fixing the problem. You're just spreading the mass across one
- 10:22Speaker 2: hundred different file I see, the boundaries of your tests
- 10:26Speaker 2: must map logically to the domain and to the developer's
- 10:29Speaker 2: mental model. If a test is chepping the shopping cart,
- 10:33Speaker 2: all the shopping cart logic needs to be structured together
- 10:36Speaker 2: in a way that makes sense. Otherwise you're sending the
- 10:39Speaker 2: next developer on a wild goose chase across fifty unrelated files.
- 10:44Speaker 1: A perfectly readable, well structured blueprint is great, but what
- 10:48Speaker 1: if the blueprint lies about the structural integrity. A beautifully
- 10:52Speaker 1: structured test is wonderful, but if that test lies to you,
- 10:56Speaker 1: it is vastly more dangerous than having no test at all.
- 11:00Speaker 2: False sets of security will sink a project faster than
- 11:02Speaker 2: anything else. We are moving from the structure of the
- 11:05Speaker 2: test to the integrity of its results, and the.
- 11:07Speaker 1: Book gives some terrifying examples of how tests can lie,
- 11:10Speaker 1: starting with something as simple as poor naming.
- 11:13Speaker 2: Yeah, the author talks about auditing a code base and
- 11:15Speaker 2: finding a test named to imply it was validating a
- 11:17Speaker 2: highly complex mask function for security. Sounds important, it does,
- 11:22Speaker 2: but when you look closely at the actual code, the
- 11:25Speaker 2: test was only checking if the parameters were a valid combination.
- 11:28Speaker 2: It wasn't testing the actual security behavior at all. If
- 11:31Speaker 2: you just looked at the test report, you'd think the
- 11:33Speaker 2: mask feature was secure when its core logic was completely untested.
- 11:37Speaker 1: Then there is the happy test. This happens when developers
- 11:41Speaker 1: are pressured by management to increase their code coverage metrics
- 11:44Speaker 1: like we talked about earlier. Tell us how the happy
- 11:47Speaker 1: test fakes success.
- 11:49Speaker 2: Well, They write a test that runs the production code,
- 11:53Speaker 2: but they wrap the whole thing in what's called a
- 11:55Speaker 2: trycatch block, and they don't write a single assertion.
- 11:58Speaker 1: Okay, for the non coder's listening, break down those two terms.
- 12:01Speaker 1: What is a tricatch block and what is an assertion?
- 12:04Speaker 2: Think of a tricatch block as a piece of code
- 12:07Speaker 2: that says, if this system crashes, just sweep the air
- 12:11Speaker 2: under the rug and act like nothing happened. It catches
- 12:13Speaker 2: the error before it fails the test and the assertion.
- 12:16Speaker 2: An assertion is the actual question you are asking. It says,
- 12:18Speaker 2: if I input two plus two, assert that the answer
- 12:22Speaker 2: is for A happy test runs the math, sweeps any
- 12:25Speaker 2: crashes under the rug and never actually asks what the
- 12:28Speaker 2: answer is for.
- 12:29Speaker 1: So it looks beautiful on paper. The boss sees ninety
- 12:32Speaker 1: nine percent coverage and is thrilled, But the test will
- 12:36Speaker 1: only fail if the server literally catches fire. It completely
- 12:39Speaker 1: ignores whether the logic is actually correct.
- 12:42Speaker 2: It's pure noise designed to satisfy a metric. Then you
- 12:46Speaker 2: have the issue of interdependent tests, which is a massive
- 12:49Speaker 2: trust breaker. Tests often secretly rely on the environment. Oh so,
- 12:53Speaker 2: let's say you have tests A which creates a user
- 12:55Speaker 2: in a database, and test B, which checks that user's permissions.
- 12:59Speaker 2: They run fine in that order. But let's say the
- 13:01Speaker 2: test framework decides to run them in alphabetical order. Oh no,
- 13:05Speaker 2: Test B runs first, tries to check a user that
- 13:07Speaker 2: hasn't been created yet, and fails.
- 13:09Speaker 1: A massive false alarm. The code itself isn't broken, the
- 13:12Speaker 1: environment is just flaky exactly.
- 13:15Speaker 2: The book mentions a real world example where Java seven
- 13:17Speaker 2: updated its internal plumbing. It changed the default sorting order
- 13:21Speaker 2: of how methods were returned, something called method reflection. Suddenly,
- 13:25Speaker 2: massive test sites broke everywhere.
- 13:27Speaker 1: Just because of an internal update.
- 13:29Speaker 2: The production code was fine, but the tests were secretly
- 13:32Speaker 2: relying on the assumption that things would always be returned
- 13:34Speaker 2: in the old order.
- 13:35Speaker 1: And speaking of assumptions, the author goes on a tear
- 13:38Speaker 1: about relying on the system clock or network connections, and
- 13:42Speaker 1: tests using commands to check the real time, like system
- 13:46Speaker 1: dot current time millis.
- 13:47Speaker 2: Using real time in a test guarantees intermittent failures. If
- 13:51Speaker 2: your test calculates a route based on current traffic, it
- 13:55Speaker 2: might pass at two point zero pm but fail at
- 13:58Speaker 2: five zero pm during rush hour.
- 14:00Speaker 3: That sounds infuriating, it is It forces developers into this
- 14:04Speaker 3: horrible habit of running a test suite four or five
- 14:07Speaker 3: times until they get lucky and it randomly passes just
- 14:10Speaker 3: to get a green light.
- 14:11Speaker 1: So what does this all mean? If my tests are
- 14:14Speaker 1: randomly failing fifty percent of the time because of an
- 14:16Speaker 1: unpredictable clock or a dropped network connection, how on earth
- 14:20Speaker 1: am I supposed to test time sensitive or internet reliant code.
- 14:24Speaker 2: That is the million dollar question for any software engineer.
- 14:27Speaker 2: To test the unpredictable, we have to completely isolate the
- 14:30Speaker 2: code from its unpredictable environment. This brings us to the
- 14:34Speaker 2: developer's secret weapon, test doubles.
- 14:36Speaker 1: The text actually quotes Mahatma Gandhi here, which I was
- 14:39Speaker 1: definitely not expecting in a software engineering book. Be the
- 14:43Speaker 1: change you want to see in the world.
- 14:45Speaker 2: Surprisingly fitting in testing, you become the change you want
- 14:49Speaker 2: to see in your code. By replacing external, unreliable dependencies
- 14:53Speaker 2: with controlled, predictable environments, you swap out the messy real
- 14:58Speaker 2: world for a test.
- 15:00Speaker 1: The book outlines five distinct reasons you would want to
- 15:03Speaker 1: use a double The first is isolating the code so
- 15:06Speaker 1: it's not dragged down by outside factors. The second is
- 15:09Speaker 1: speeding up execution.
- 15:10Speaker 2: Speed is so important, right, say you have.
- 15:13Speaker 1: A piece of code that runs a really slow weighted
- 15:15Speaker 1: graph routing algorithm. If every single test has to wait
- 15:18Speaker 1: for a complex GPS calculation, your test suite will take hours.
- 15:23Speaker 1: You use a double to instantly return a pre calculated route.
- 15:26Speaker 2: Because speed is crucial for that fast feedback loop. The
- 15:29Speaker 2: third reason is making things deterministic. Think of a craps game.
- 15:33Speaker 2: If the dice are truly random, you can't write a
- 15:36Speaker 2: test for what happens when a player rolls snake eyes
- 15:39Speaker 2: because you might have to run the test one hundred
- 15:41Speaker 2: times to get that specific roll. A test double lets
- 15:44Speaker 2: you rig the dice to roll snake eyes on command.
- 15:48Speaker 1: Which naturally leads into the fourth reason, simulating impossible conditions.
- 15:52Speaker 1: There's a hilarious anecdote in the text about trying to
- 15:55Speaker 1: test how an app handles a dropped Internet connection.
- 15:58Speaker 2: Oh yeah, short of programming a Lego Mindstorm's robot to
- 16:02Speaker 2: physically drive over and unplug your computer's network cable at
- 16:06Speaker 2: the exact right.
- 16:07Speaker 1: Millisec which would be amazing to watch but.
- 16:09Speaker 2: Very impractical, Yeah, it is nearly impossible to automate a
- 16:12Speaker 2: dropped connection test in the real physical world. A test
- 16:16Speaker 2: double lets you simulate that network failure perfectly in software
- 16:20Speaker 2: every single time.
- 16:21Speaker 1: And the fifth reason is exposing hidden information, letting a
- 16:25Speaker 1: test check internal states without ruining the actual code design
- 16:29Speaker 1: to get to it. This whole concept makes total sense.
- 16:32Speaker 1: If you're shooting an action movie, you don't throw your
- 16:34Speaker 1: lead actor out.
- 16:35Speaker 2: Of a helicopter I hope not.
- 16:37Speaker 1: You use a stunt double. The camera gets the shot,
- 16:39Speaker 1: the story moves forward, but nobody actually dies exactly.
- 16:44Speaker 2: You get the validation without the real world risk or delay.
- 16:47Speaker 2: But just like in movies, a car chase needs a
- 16:50Speaker 2: different type of stunt double than a martial arts.
- 16:52Speaker 1: Fight scene, which brings us to the specific taxonomy of
- 16:55Speaker 1: test doubles based on what the developer needs to achieve.
- 16:59Speaker 1: Let's go through the cast of characters, starting with test stubs.
- 17:03Speaker 2: A test stub is the simplest form. As the dictionary
- 17:06Speaker 2: definition of the book says, it's an unusually short thing.
- 17:10Speaker 2: It stands in with the simplest possible implementation.
- 17:13Speaker 1: Give us an example.
- 17:14Speaker 2: For example, if your code is supposed to log an
- 17:16Speaker 2: event to a remote server, you might use a stublogger
- 17:19Speaker 2: that just instantly returns a hard coded warning message and
- 17:22Speaker 2: does absolutely nothing else. It satisfies the code's request without
- 17:26Speaker 2: triggering a massive network operation.
- 17:28Speaker 1: Okay, next flavor up fake objects.
- 17:31Speaker 2: A fake object is a step up because it actually
- 17:33Speaker 2: contains real logic, but it operates without the real world
- 17:36Speaker 2: side effects. A great example is an in memory user repository.
- 17:40Speaker 1: How does that differ from a stub?
- 17:42Speaker 2: Instead of connecting to a heavy, slow, real world database
- 17:45Speaker 2: to save a user, the fake object just saves the
- 17:49Speaker 2: user data to a simple list in the computer's temporary memory.
- 17:53Speaker 2: It quacts like a database. It stores data like a database,
- 17:56Speaker 2: but it is vastly faster and doesn't require a network connection.
- 18:00Speaker 1: Love that third on the list. Test spies.
- 18:04Speaker 2: Test spies are used in tricky situations where a method
- 18:06Speaker 2: doesn't return to value you can check. Say you have
- 18:08Speaker 2: a distributed log that sends messages to multiple targets. Okay,
- 18:11Speaker 2: you can't just ask the log what it returned, so
- 18:13Speaker 2: you pass in a test spy. The spy silently records
- 18:17Speaker 2: every single message it receives. Then, after the code runs,
- 18:20Speaker 2: the test debreachs the spy and says what did you see?
- 18:23Speaker 2: Did you get the correct.
- 18:23Speaker 1: Message, which leads to the final and most strict flavor.
- 18:27Speaker 2: Mock objects mark objects are the ultimate control freaks of
- 18:30Speaker 2: the test double world. You program them with extreme precision
- 18:34Speaker 2: before the test even runs. For instance, you might create
- 18:37Speaker 2: a mock Internet object and you tell it expect exactly
- 18:40Speaker 2: one call to this specific method and expect these exact
- 18:43Speaker 2: specific parameters.
- 18:44Speaker 1: Here's where it's really interesting. So a stub is a
- 18:47Speaker 1: cardboard cutout, a spy is an undercover cop taking notes.
- 18:51Speaker 1: But a mock object is like a cyborg terminator. You
- 18:55Speaker 1: program its mission before you send it back in time.
- 18:58Speaker 1: You tell it John Connor will ask you for help
- 19:00Speaker 1: exactly one time.
- 19:01Speaker 2: I love the terminator analogy, right.
- 19:03Speaker 1: If John asks twice, or if Sarah Connor asks instead,
- 19:07Speaker 1: the mock instantly triggers an alarm and fails the test.
- 19:11Speaker 1: It enforces absolute rigid adherence to the expected.
- 19:14Speaker 2: Interaction that maps perfectly to the mechanism. If the code
- 19:18Speaker 2: calls that mock Internet object zero times, it instantly fails
- 19:21Speaker 2: the test. If it calls it two times, it fails.
- 19:24Speaker 2: If the parameters are off by one character, it fails.
- 19:26Speaker 2: It ensures the system behaves exactly is designed with zero deviation.
- 19:30Speaker 1: So to pull all of this together for the listener.
- 19:32Speaker 1: Writing good tests is not a chore. It is not
- 19:34Speaker 1: just about keeping the boss happy with a coverage metric.
- 19:37Speaker 1: It is a profound design exercise. It forces you to
- 19:40Speaker 1: think about how your code behaves in isolation, how it
- 19:43Speaker 1: handles the unpredictable, and how it communicates its intent clearly
- 19:46Speaker 1: to the next person who has to read it. Good
- 19:48Speaker 1: tests save time, they save money, and frankly, they save
- 19:50Speaker 1: your sanity.
- 19:51Speaker 2: If we connect this to the bigger picture, treating code
- 19:55Speaker 2: testing with this level of rigor fundamentally changes how you
- 19:59Speaker 2: view all common complex systems.
- 20:00Speaker 1: It really does.
- 20:02Speaker 2: When you realize how easily a system can be derailed
- 20:05Speaker 2: by a hidden dependency or a false assumption, you start
- 20:08Speaker 2: demanding better verification everywhere. You stop trusting the happy test
- 20:14Speaker 2: in your business processes, in your supply chains, and in
- 20:17Speaker 2: your organizational structures. You start looking for the empty method
- 20:20Speaker 2: stubs in your own workflows.
- 20:22Speaker 1: I want to leave you with a final thought to
- 20:24Speaker 1: mole over taking the source material just one step further
- 20:28Speaker 1: into everyday life. Think about applying the concept of test
- 20:31Speaker 1: driven development, defining the success criteria before you act to
- 20:35Speaker 1: your own life.
- 20:36Speaker 2: That's a fascinating way to look at it.
- 20:38Speaker 1: What if before starting a major home project, before sitting
- 20:41Speaker 1: down for a difficult conversation with a partner, or before
- 20:44Speaker 1: making a massive career decision, you first define the exact
- 20:47Speaker 1: tests that would prove you succeeded.
- 20:49Speaker 2: Writing the mole before you pour the concrete of your
- 20:51Speaker 2: life's next chapter.
- 20:53Speaker 1: How much accidental complexity, how much unnecessary drama and wasted
- 20:57Speaker 1: effort would you avoid if you knew exactly what passing
- 20:59Speaker 1: life like before you wrote the first line. At the
- 21:02Speaker 1: start of this deep dive, we talked about that broken
- 21:05Speaker 1: X ray machine, the murky diagnostic waters of trying to
- 21:08Speaker 1: fix a problem after it has already become a tangled mess.
- 21:11Speaker 1: TDD is essentially building a perfect, customized X ray machine
- 21:15Speaker 1: for a specific bone before you even take the step
- 21:17Speaker 1: that might cause the break. Keep asking questions, keep defining
- 21:20Speaker 1: your success criteria, and keep tuning in for the next
- 21:23Speaker 1: deep dive.