Part 10 / Topic 3 of 12
Your first test
Real-world example: examples/bank-and-discount/DiscountCalculator.java — the same pricing idea as Stage 1 Part 5's finalPrice, now with input validation so there is something worth testing beyond the happy path.
public class DiscountCalculator {
static final double GST_RATE = 0.18;
public static double finalPrice(double price) {
return finalPrice(price, 0.0);
}
public static double finalPrice(double price, double percentOff) {
if (price < 0) {
throw new IllegalArgumentException("price cannot be negative: " + price);
}
if (percentOff < 0 || percentOff > 100) {
throw new IllegalArgumentException("percentOff must be between 0 and 100: " + percentOff);
}
double discounted = price - (price * percentOff / 100);
return withGst(discounted);
}
static double withGst(double price) {
return round2(price * (1 + GST_RATE));
}
static double round2(double value) {
return Math.round(value * 100) / 100.0;
}
}
A first test, in its own file, DiscountCalculatorTest.java:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DiscountCalculatorTest {
@Test
void finalPrice_noDiscount_addsGst() {
double result = DiscountCalculator.finalPrice(1000.0);
assertEquals(1180.0, result, 0.001);
}
}
@Testmarks a method as a test JUnit should run — without it, a method is just a method, never executed by the launcher.assertEquals(expected, actual, delta)checks the two numbers are equal (withindelta, see Section 5 for why).expectedalways comes first — the failure message below only reads correctly because of this order.- The test class itself needs no
publicand nomain— package-private is enough, and JUnit, not you, calls these methods.
Compile and run it exactly as Section 2 showed, from inside examples/bank-and-discount/:
$ javac -cp ../lib/junit-platform-console-standalone-1.14.5.jar DiscountCalculator.java DiscountCalculatorTest.java
$ java -jar ../lib/junit-platform-console-standalone-1.14.5.jar execute -cp . --scan-class-path --details=tree --disable-ansi-colors
If you type only the one test above into a fresh DiscountCalculatorTest.java, this is what you will see:
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│ └─ DiscountCalculatorTest ✔
│ └─ finalPrice_noDiscount_addsGst() ✔
└─ JUnit Vintage ✔
Test run finished after 21 ms (varies)
[ 4 containers found ]
[ 1 tests found ]
[ 1 tests successful ]
[ 0 tests failed ]
(The "Thanks for using JUnit!" line is the launcher's own sponsorship banner — it prints before every execute run, every time; it is not an error. Counts below it are trimmed to the ones that matter; the real run also prints container/skip/abort lines, all zero here.) "JUnit Vintage" is an empty engine for old JUnit 4 tests — it shows up even when you have none, and that is normal. The DiscountCalculatorTest.java actually committed in examples/bank-and-discount/ is the cumulative file this whole lesson builds — it already carries the tests Sections 4–7 add. Run the same two commands against that real file and you will see all of it at once, 6 containers and 12 tests — the exact tree is in Section 12.
Full programs: examples/bank-and-discount/DiscountCalculator.java, examples/bank-and-discount/DiscountCalculatorTest.java.
Done here? The practice set for Part 10 — including the solutions — is on the part page.