Notes
The problem
Look at the coupon box in the ShopRung cart. Three requirements touch it. An unknown code is refused. WELCOME15 has expired and must be refused with its own message. SAVE10 only works from a subtotal of fifty dollars. Each rule is simple on its own. The hard questions are about combinations. What happens when a code is real, but the cart is too small? Testing each rule once, in isolation, will not answer that.
- PRICE-7 unknown code: "No coupon called <CODE>"
- PRICE-5 WELCOME15 expired: "This coupon has expired"
- PRICE-3 SAVE10 needs a subtotal of $50.00 or more
- which message wins when two rules are true at once?
The idea
A decision table is a grid. Conditions go in the top rows, things that are true or false, like the email has an account. Actions go in the bottom rows, the things the system does. Each column is a rule: one combination of conditions and the actions it must cause. Here is ShopRung login. Two conditions give four rules. Only the first logs you in. The other three show the same message, which is exactly what rule account six asks for.
R1 R2 R3 R4 email has account Y Y N N password correct Y N Y N ------------------------------- log in X show "Email or password is wrong" X X X 2 conditions = 4 rules
The vocabulary
A few words you will hear in interviews. A condition is an input or a state that is either true or false. An action is something the system does. A rule is one column. In the cells you write Y for true and N for false, and a dash when that condition does not matter for this rule. An X marks the actions that happen. Tables with only Y and N are called limited entry. When a cell holds a value or a range instead, it is extended entry.
- condition: an input or state that is true or false
- action: what the system does
- rule: one column, one combination + its actions
- Y / N: true / false
- : "does not matter here"
- X : this action happens
- extended entry: values such as "< $50" instead of Y/N
Step 1 · Conditions
Step one. Read the requirements and turn each decision into a yes or no question. For the coupon box there are three. Does the code exist? Has it expired? Is the subtotal at least the coupon's minimum? The actions are: take the discount off, and show a message. Notice what is not a condition. Lowercase save ten works the same as uppercase, because rule price two says codes are not case sensitive. That is a partition to test, not a new row.
- conditions C1 the code exists C2 the code has expired C3 subtotal >= its minimum
- actions A1 take the discount off A2 show the right message
- codes are not case-sensitive (PRICE-2): save10 = SAVE10
Step 2 · Count rules
Step two. Count the rules. Each yes or no condition doubles the number of combinations, so three conditions give two times two times two, which is eight. Write all eight columns in a fixed pattern, so you cannot miss one: the first row flips every four columns, the next every two, the last every column. Then fill in the actions for each rule. Only rule three, a real code that has not expired with a big enough cart, gives the discount.
R1 R2 R3 R4 R5 R6 R7 R8 exists Y Y Y Y N N N N expired Y Y N N Y Y N N enough Y N Y N Y N Y N ----------------------------------- discount X expired msg X X needs $ msg X unknown msg X X X X
Step 3 · Merge
Step three. Merge. Look at the four columns where the code does not exist. Expired or not, enough or not, the answer is always no coupon called that code. So those four become one rule with dashes. The same happens for an expired code: the subtotal does not change the answer. Eight rules become four. Only merge columns whose actions are identical. And write down the order of checks you assumed, because the requirements rarely say which message wins.
R1 R2 R3 R4
exists N Y Y Y
expired - Y N N
enough - - N Y
------------------------------------
action unknown expired needs 10%
msg msg $50 off
merge only when every merged
column has the same actions Step 4 · Test cases
Step four. Turn each rule into a test case with real data. Rule one, the code SAVE20 on a sixty dollar cart, four green mugs. Expect no coupon called SAVE20. Rule two, welcome fifteen in lowercase on the same cart. Expect this coupon has expired, and you have checked case too. Rule three, SAVE10 with one hoodie at forty nine ninety nine. Expect the needs fifty dollars message. Rule four, four notebooks at exactly fifty dollars. Expect five dollars off.
R1 SAVE20 at $60.00 → "No coupon called SAVE20" R2 welcome15 at $60.00 → "This coupon has expired" R3 SAVE10 at $49.99 (1 hoodie) → "...at least $50.00" R4 SAVE10 at $50.00 (4 notebooks) → $5.00 off $60.00 = 4 Green Build Mugs
Run them
Now run them. In the shop you type the code and read the message. Through the API you can check more. Rule two returns status code four hundred, with the error coupon not applicable and the message this coupon has expired. Rule one, the unknown code, returns four hundred and four with unknown coupon. Rule four returns the cart with a discount of five dollars. One table gave you four tests, and each one checks a different branch of the code.
POST /api/cart/coupon
{"code": "welcome15"}
HTTP/1.1 400 Bad Request
{"error": "coupon_not_applicable",
"message": "This coupon has
expired"}
R1 SAVE20 → 404 unknown_coupon Impossible rules
A second table, for SAVE10 and shipping. Rule price nine says shipping is free when the subtotal after the discount is seventy five dollars or more. Two conditions: is the subtotal at least fifty, and is the amount after the discount at least seventy five. Look at rule two. If the subtotal is under fifty, there is no discount, so the amount after it is also under fifty. It can never reach seventy five. That rule is infeasible. Mark it, and do not try to test it.
R1 R2 R3 R4 subtotal >= $50 N N Y Y after 10% >= $75 N Y N Y ---------------------------------- take 10% off X X shipping $4.99 X X shipping free X R2: impossible. No discount, so "after" is under $50 too
The surprise
Now the feasible rules with real carts. A thirty dollar cart is refused, pays shipping, total thirty four ninety nine. A keyboard plus a sticker pack is seventy nine ninety nine. Without a coupon, shipping is free. With SAVE10 the customer saves eight dollars, drops to seventy one ninety nine, and now pays four ninety nine shipping. Total seventy six ninety eight. That is correct by the rule, but surprising. Decision tables find exactly these questions to raise with the product owner.
R1 $30.00, SAVE10 refused shipping $4.99 total $34.99 R3 $79.99 (keyboard + stickers) -$8.00 = $71.99, ship $4.99 total $76.98 no coupon: free ship, $79.99 R4 $100.00 (8 notebooks) -$10.00 = $90.00, free ship total $90.00
Automate the table
A decision table maps straight onto a parameterized test. In JUnit five, each rule becomes one row of a CSV source: the code, the subtotal in cents, and the expected result. A small helper resets the shop, builds a cart worth that subtotal, posts the code and returns what happened. The name attribute prints rule one, rule two and so on in the report, so a failure points straight back at a column in your table. Add a rule, add a row.
@ParameterizedTest(name = "rule {index}")
@CsvSource({
"SAVE20, 6000, unknown_coupon",
"welcome15, 6000, coupon_not_applicable",
"SAVE10, 4999, coupon_not_applicable",
"SAVE10, 5000, applied" })
void couponRule(String code, int cents,
String expected) {
assertEquals(expected,
applyCoupon(code, cents));
} Rules of thumb
Some rules of thumb. The table doubles with every condition. Four conditions give sixteen rules, ten give over a thousand. So once you pass four or five conditions, split the decision into smaller tables, for example one for which coupon applies and one for shipping. Merge with dashes, then recount to be sure no combination vanished. And measure decision table coverage as the rules you tested divided by the rules that are feasible. Aim for all of them.
- conditions → full rules 2 → 4 3 → 8 4 → 16 5 → 32 10 → 1,024
- over 4 or 5 conditions: split into smaller tables
- merge with dashes, then check no rule was lost
- coverage = rules tested / feasible rules
Common mistakes
The common mistakes. Testing each rule on its own and never the combinations, which is the whole point of the table. Merging columns whose actions are not the same, which quietly deletes a test. Spending an hour on an impossible combination and then logging it as a bug. Writing rules with no concrete values, so nobody can tell whether the test passed. And not asking which message wins when two rules clash. That last one is a requirement gap, and finding it early is a win.
- testing each rule alone, never the combinations
- merging columns whose actions differ
- testing an impossible rule, then logging a "bug"
- no concrete values, so the test cannot fail clearly
- not asking which message wins when two rules clash
Say it like this
The interview question is usually: how would you test a feature with several business rules? Say this. I list the conditions and the actions from the specification. Three yes or no conditions give eight rules. I merge columns that have the same outcome using dashes, mark any combinations that cannot happen, and write one test case per remaining rule with concrete data. For a coupon box, that came to four tests, and it also exposed a gap: the spec never said which message wins.
- "I list the conditions and actions from the spec. Three yes/no conditions give 8 rules. I merge columns with the same outcome using dashes, mark infeasible ones, and write one test per remaining rule. For a coupon box that was 4 tests."