简体   繁体   中英

How would I write a JUnit test for this code?

hello I'm new to java and I'm confused on how I should write JUnit test can someone help?

The task instructions:

When you use an automated teller machine (ATM) with your bank card, you need to use a personal identification number (PIN) to access your account. If a user fails more than three times when entering the PIN, the machine will block the card.

Assume that the user's PIN is “1234” and write a program that asks the user for the PIN no more than three times, and does the following:

  • If the user enters the right number, print a message saying, “Your PIN is correct”, and end the program.
  • If the user enters a wrong number, print a message saying, “Your PIN is incorrect” and, if you have asked for the PIN less than three times, ask for it again.
  • If the user enters a wrong number three times, print a message saying “Your bank card is blocked” and end the program
import java.util.Scanner;

public class BankingSystemWithPin {
    
    private static int pinAttempts = 0;
    
    public static void main(String[] args) {
        BankAccount bankAccount1 = new BankAccount("RO29455302311322", 200, "1234");
        BankAccount bankAccount2 = new BankAccount("RO43593530521134", 600, "7530");
        BankAccount[] bankAccounts = {bankAccount1, bankAccount2};
        Scanner scanner = new Scanner(System.in);
        BankAccount currentBankAccount = null;
        while (currentBankAccount == null) {
            pinAttempts++;
            if (pinAttempts > 3) {
                System.err.println();
                System.err.println("You have exceeded the allowable number of login attempts!");
                System.err.println("The transaction is over. ");
                System.exit(0);
            }
            Menu.welcome();
            String pin = scanner.nextLine();
            currentBankAccount = BankingSystemWithPin.getBankAccountByPin(bankAccounts, pin);
        }
        
        // PIN correct and account number acquired!
        ATM.useATM(currentBankAccount);
    }
    
    public static BankAccount getBankAccountByPin(BankAccount[] bankAccounts, String pin) {
        for (BankAccount bankAccount : bankAccounts) {
            if (bankAccount.pin.equals(pin)) {
                System.out.println();
                System.out.println("Account Number: " + bankAccount.IBAN);
                return bankAccount;
            } 
        }
        System.err.println("Wrong Pin. Try Again.");
        System.err.println();
        return null;
    }
}

Just s few pointers;

  1. Move the logic currently in static methods into class methods - possibly into the ATM class. Static methods are notoriously difficult to test
  2. Separate out the user interaction from the pin validation logic. The ATM class should have a method like validatePin(pinNumber) that knows nothing about how the PIN was obtained.
  3. Give your ATM class an internal counter that spits the dummy when a third call to validatePin(pinNumber) fails. It could throw a custom exception when three tries fail.

That done, it should be simple to at least test the ATM class.methods. Testing user input logic is somewhat more difficult and is often done manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM