What to testeasy0-2 years

A pull request adds a test for `Checkout`: it mocks `Discount`, stubs `apply(6000)` to return 5400, asserts that `checkout.total(6000)` is 5400, and verifies that `apply(6000)` was called. Coverage goes up and the build is green. The rule is 'orders **over** 5000 paise get 10% off', and the real `Discount` says `>=`. What does this test prove, and what would you write instead?

It proves that Mockito returns what it was told to return. The mock is the discount, so the real Discount.apply never runs, and the test would stay green if the rule returned a random number. It counts towards coverage exactly like a useful test would. The test to write uses the real Discount and sits on the boundary the rule is about: checkout.total(5000) must be 5000, because 'over' excludes 5000. That test fails against >= with expected: <5000> but was: <4500>, which is the bug. The rule behind it: mock at a boundary you do not want to cross in this test (the database, the payment provider, the clock), and use real objects for your own logic.

The lesson behind it →