Video in progress
What do you understand by a unit test, and what exactly does it check?
A unit test checks the smallest piece of code (usually one class or method) quickly, in isolation from the outside world. No database, network or file system is involved.
📖 Answer
A unit test answers one question: "given this input, does this class return the right result?"
class PriceCalculatorTest {
@Test
void chegirma_10_foiz_bolsa_narx_kamayadi() {
var calc = new PriceCalculator();
var result = calc.apply(new BigDecimal("200000"), 10);
assertEquals(new BigDecimal("180000"), result);
}
}
There's no Spring and no database here: the object is created with a plain new. That's why the test runs in milliseconds, and when it fails you know right away the bug is in this very class.
Signs of a good unit test: it's fast, doesn't depend on other tests, gives the same result every time, and when it fails the cause is obvious. If a test needs Docker or the internet to pass, it's no longer a unit test.
Sign up to mark studied questions and save your progress.