Add Tests to the Bank Account System
Part of the Java Roadmap
In short
Your bank account project from earlier works when you try it by hand. Now you'll make it a proper Maven or Gradle project with a test suite, the way real teams prove their code works.
Turn the earlier bank account project into a Maven or Gradle project with a JUnit 5 test suite that covers deposits, withdrawals, transfers and error cases, using Mockito for any dependencies.

Think of it like this
The bank's own audit
A bank doesn't just trust its tellers. Auditors run fixed checks: does every deposit show up, does a transfer debit one account and credit the other, is an overdraft refused? Your test suite is that audit team, and it runs in seconds every time you change the code.
The big ideas
What you need to know
Mirror the packages
BankTest goes in src/test/java/com/example/bank so it can see package-private methods of Bank.
Group with @Nested
Inner @Nested classes like Deposits and Transfers keep a large test file readable in the test report.
Edge cases earn their keep
Zero, negative, exactly the balance, one rupee more. Bugs live at the boundaries.
Run tests on every push
Add a GitHub Actions workflow that runs mvn test or ./gradlew test so a red build blocks broken code.
How it works
Project plan
- 1
Make it a build project
Create a Maven or Gradle project, move the classes into src/main/java/com/example/bank and get it compiling.
- 2
Fix the design for testing
Make sure methods throw clear exceptions for bad input instead of printing messages, and pass collaborators in through constructors.
- 3
Test each account type
Deposits, withdrawals, minimum balance rules and interest for savings and current accounts.
- 4
Test transfers and failures
Successful transfers, insufficient funds, the same account on both sides, and that a failed transfer changes nothing.
- 5
Mock and measure
Mock any notifier or repository with Mockito, then check coverage with IntelliJ's Run with Coverage or JaCoCo.
See it in code
Walk through the code
1package com.example.bank;2 3import org.junit.jupiter.api.BeforeEach;4import org.junit.jupiter.api.Nested;5import org.junit.jupiter.api.Test;6 7import static org.junit.jupiter.api.Assertions.*;8 9class BankTest {10 private SavingsAccount priya;11 private SavingsAccount arjun;12 private Bank bank;13 14 @BeforeEach15 void setUp() {16 priya = new SavingsAccount("SB-001", "Priya", 10_000);17 arjun = new SavingsAccount("SB-002", "Arjun", 2_000);18 bank = new Bank();19 }20 21 @Nested22 class Transfers {23 @Test24 void movesMoneyBetweenAccounts() {25 bank.transfer(priya, arjun, 3_000);26 assertAll(27 () -> assertEquals(7_000, priya.getBalance()),28 () -> assertEquals(5_000, arjun.getBalance()));29 }30 31 @Test32 void failedTransferChangesNothing() {33 assertThrows(IllegalStateException.class,34 () -> bank.transfer(arjun, priya, 50_000));35 assertEquals(2_000, arjun.getBalance());36 assertEquals(10_000, priya.getBalance());37 }38 }39}1/4Two fresh accounts and a Bank for every single test, including the nested ones.
A slice of the finished suite. Add similar @Nested classes for Deposits, Withdrawals and Interest.
Save yourself the pain
Common mistakes
Oops
Testing only the happy path where every transfer succeeds.
Do this instead
At least half your tests should cover failures: overdrafts, zero and negative amounts, unknown accounts.
Oops
Debiting before checking the balance, so a failed transfer leaves money missing.
Do this instead
Validate first, then change both balances. The failedTransferChangesNothing test catches this.
Oops
Chasing 100 percent coverage with tests that assert nothing.
Do this instead
Aim for meaningful assertions on every rule. Around 80 percent with real checks beats 100 with empty ones.
Your mission
Ship the tested bank app
Quick check
A transfer of ₹50,000 from an account holding ₹2,000 should fail. What should the test check?
Go deeper
- 1Convert the project to Maven or Gradle and add JUnit 5 as a test dependency.
- 2Write tests for normal cases, edge cases and exceptions with assertThrows.
- 3Run the tests on every push with a simple GitHub Actions workflow.
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.