From the course: Practical Test-Driven Development for Java Programmers
Examples and outcomes
From the course: Practical Test-Driven Development for Java Programmers
Examples and outcomes
- [Instructor] Hello again. We are now ready to start writing code, which, of course, means writing some tests to drive that code first. Now a quick glance at the Wikipedia page for ISBN numbers tells us that there's quite a bit here that we want to consider. If you read through this page, you'll discover that ISBN numbers can be either 10 digits long or 13 digits long and there is a different calculation for 10 digits and 13 digits. So there's going to be quite a lot to do here. We need to start somewhere, it doesn't really matter where, as we're going to keep building and building until we have all the functionality covered. So let's start with a 10 digit example. Now if you look at Amazon's website for any book, you can find out its ISBN number. For example, I've got the Odyssey by Homer on screen here and if I scroll down this page to this section here, we'll find out the ISBN number for this book. Actually, we'll see both the 10 digit and the 13 digit ISBN number. Right now, I'm just interested in this 10 digit one which you can see on screen right here. So we know that this is an example of a valid ISBN number. So I'd like to start by writing a test to say if we are trying to validate this number then we expect our ISBN validator code should return that this is a valid number. We're going to make that our first test. Actually, let's just pause for a moment and talk about why this is going to be our first test. When we write tests, we're thinking about the expected outcomes, not the logic, behavior, the code, the way we might implement it, the architecture, the design, well that list just goes on and on. Tests generally will work on the basis of I expect this particular example to result in the following outcomes. By using lots of tests with lots of examples that's how we'll know we've covered all the logic that we need. So a good place to start is with an example where we want the outcome to be this is a valid ISBN. It doesn't matter which example we use for this first test because we're going to add more examples later, but for now, saying here's an ISBN, which we know is valid. Well, that's a good example to start with. So let's test that if we use the ISBN we just discovered from Amazon, our code will say it's valid. I'm laboring the point here, maybe, but it's an important aspect to understand. When you're writing tests, think about examples and outcomes not code or how it should work in detail.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.