Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Assertions Practice | Unit Tests
Java JUnit Library. Types of Testing

Assertions Practice

Scorri per mostrare il menu

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);
    }
}
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 7

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Sezione 2. Capitolo 7
some-alt