Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Assertions Practice | Unit Tests
Java JUnit Library. Types of Testing

Assertions Practice

Свайпніть щоб показати меню

It's time to practice with different Assertions. A class has been written for you with some methods. Tests for these methods are already written, but for some reason, they keep failing. Your task is to find out the reason for this. The problem lies in the tests, not the code.

Link to the Task

Pay close attention to the expected and actual values. This way, you will understand what needs to be used in a specific test case.

Hint
expand arrow

Your goal is to make the tests work, check the assertions for correctness.

Some test cases have two assertions at once; pay attention to that.

You will need to use almost every assertion from the previous chapter, so be attentive.

Solution
expand arrow
public class BankTransactionTest {

    @Test
    public void testInitialBalancePositive() {
        double initialBalance = 100.0;
        BankTransaction bt = new BankTransaction(initialBalance);
        double expected = 100.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
    }

    @Test
    public void testInitialBalanceNegative() {
        double initialBalance = -100.0;
        BankTransaction bt = new BankTransaction(initialBalance);
        double expected = 0.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
        assertNotEquals(initialBalance, actual, 0.001);
    }

    @Test
    public void testDeposit() {
        BankTransaction bt = new BankTransaction(100.0);
        double depositAmount = 50.0;
        boolean depositResult = bt.deposit(depositAmount);
        assertTrue(depositResult);

        double expected = 150.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
    }

    @Test
    public void testDepositNegativeAmount() {
        BankTransaction bt = new BankTransaction(100.0);
        double depositAmount = -50.0;
        boolean depositResult = bt.deposit(depositAmount);
        assertFalse(depositResult);

        double expected = 100.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
    }

    @Test
    public void testWithdrawSuccessful() {
        BankTransaction bt = new BankTransaction(100.0);
        double withdrawalAmount = 50.0;
        boolean withdrawalResult = bt.withdraw(withdrawalAmount);
        assertTrue(withdrawalResult);

        double expected = 50.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
    }

    @Test
    public void testWithdrawInsufficientFunds() {
        BankTransaction bt = new BankTransaction(50.0);
        double withdrawalAmount = 100.0;
        boolean withdrawalResult = bt.withdraw(withdrawalAmount);
        assertFalse(withdrawalResult);

        double expected = 50.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
    }

    @Test
    public void testWithdrawNegativeAmount() {
        BankTransaction bt = new BankTransaction(100.0);
        double withdrawalAmount = -50.0;
        boolean withdrawalResult = bt.withdraw(withdrawalAmount);
        assertFalse(withdrawalResult);

        double expected = 100.0;
        double actual = bt.getBalance();
        assertEquals(expected, actual, 0.001);
    }

    @Test
    public void testNotNullBankTransaction() {
        BankTransaction bt = new BankTransaction(100.0);
        assertNotNull(bt);
    }

    @Test
    public void testNullBankTransaction() {
        BankTransaction bt = null;
        assertNull(bt);
    }
}
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 7

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 2. Розділ 7
some-alt