How many scenarios do we need to test for this method?
getCakePrice determines what to charge for a cake. It has 3 parameters:
size // String, either "1-10 guests" or "11-20 guests"
writing // String, max 30 characters, blank allowed
pickupTime // java.util.Date object
(writing is what we'll write on the cake.)
Here's how the values are set by the user:
-
size is a radio button. 1-10 guests is selected by default.
-
writing is a text input field, limited to 30 characters. The field is blank by default.
-
pickupTime is selected from a calendar, limited to times when the bakery is open. The calendar does not go back in time. Tomorrow at 6:00 pm is selected by default.
And here’s how we determine the cost of a cake:
- $45 for 1 to 10 guests
- $75 for 11 to 20 guests
- +$15 for writing and same day pickup
Let’s start by looking at the possible values for each parameter.
The size of the cake is easy—it can only be one of two options, ‘1-10 guests’ or ‘11-20 guests’. The radio button with a default is nice because we know we don’t have to worry about invalid or blank input.
But wait—do we know we don’t have to worry about invalid or blank input? We got specifications of the field types and defaults in the UI, but what if we have an API and someone directly hits it?
This is a good question to ask your interviewer because it shows an ability to think of things that could go wrong. In this case, the spirit of the question doesn’t involve catching edge cases, so we'll assume the arguments are reliably and intentionally restricted, and you can count on valid input.
The writing on the cake can be blank or present. We don’t have to worry about it being too long because the input is already limited to 30 characters. We might want to ask our interviewer if we allow any 30 characters. What about obscure special characters or offensive words? We’ll say there’s no restriction—these problems are rare enough and messy enough that we can just follow up by phone if we’re concerned about the message.
For pickup time, looks like we only have 2 values to look for. Times today or times later than today. We know the input is a calendar, has a default, and doesn’t allow times when the bakery is closed. So we don’t have to worry about blank or invalid input. Although a reasonable question would be to ask about a limit in the future—how far in advance can someone order a cake? We’ll say there’s no limit here.
Ok, let’s lay this all out. What are all the combinations of inputs we can have?
Well, we have 2 possibilities for size, 2 possibilities for writing, and 2 possibilities for pickup time. So that’s 8 combinations (2 x 2 x 2 = 8).
Size |
1-10 |
1-10 |
1-10 |
1-10 |
11-20 |
11-20 |
11-20 |
11-20 |
Writing |
Blank |
Blank |
Present |
Present |
Blank |
Blank |
Present |
Present |
Same day |
✓ |
✕ |
✓ |
✕ |
✓ |
✕ |
✓ |
✕ |
We know enough to answer the question now, but let’s take it a step further and look at the price for each scenario.
Size |
1-10 |
1-10 |
1-10 |
1-10 |
11-20 |
11-20 |
11-20 |
11-20 |
Writing |
Blank |
Blank |
Present |
Present |
Blank |
Blank |
Present |
Present |
Same day |
✓ |
✕ |
✓ |
✕ |
✓ |
✕ |
✓ |
✕ |
Price |
45 |
45 |
60 |
45 |
75 |
75 |
90 |
75 |
This kind of table—showing the possible combination of values for a set of conditions, and identifying the actions we take—is called a decision table. This kind of thinking will get even more helpful as the total number of parameters and possible values grows. What if we had different prices for different flavors? What if the inputs could be blank or invalid? Decision tables help us methodically design comprehensive tests.
Sometimes decision tables can also help combine scenarios that have the same action (for example, we could combine the first 2 columns and say "if a cake is for 1-10 guests and writing is blank, it doesn’t matter what the pickup time is—we charge $45"). But this can be dangerous for testing—what if a bug factors in the pickup time when it shouldn’t?
So for this this method, we should test 8 scenarios—every possible combination of inputs.