Video in progress

Junior 🧪 Testing

How does the Arrange-Act-Assert (or Given-When-Then) pattern structure a test?

The test is split into three parts: preparing the data and objects, performing the one action under test, and checking the result. That way you can see at a glance what situation the test covers.

📖 Answer

@Test
void balansdan_kop_pul_yechib_bolmaydi() {
    // given
    var account = new Account("UZ01", new BigDecimal("50000"));

    // when
    var result = account.withdraw(new BigDecimal("70000"));

    // then
    assertFalse(result.isSuccess());
    assertEquals(new BigDecimal("50000"), account.getBalance());
}

The practical benefit: someone reading the test finds the answers to "what was set up, what was called, what was expected" in three blocks.

As a rule, it's good for the "when" part to be a single line. If three different methods are being called there, you probably have three tests, not one.

The comments (// given) aren't required; separating the parts with a blank line is enough. What matters is that the asserts aren't scattered before or in the middle of the act.