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

Assertions Practice

Stryg for at vise menuen

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);
    }
}
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Sektion 2. Kapitel 7
some-alt