lundi 28 octobre 2019

How to initialize an object once and use it for all test cases

I have a test class where I have all my test cases for my java project, I want to initialize the service class once which creates an array and I want to use the same array for all the other test cases, I have tried to do that but when the first test case is ran an customer is registered and stored in the array I want to use the customer stored in the array and use it for the next test case but it seems that the customer is not in the array for the next test case i.e a new array is being created or I think that junit is running each test case individually

MainTests.java

package tests;

import static org.junit.Assert.*;

import org.junit.*;

import services.BankingServiceImpl;
import dao.BankingSystemArrayImpl;

public class MainTests {

    static BankingServiceImpl bankingService;
    private static boolean setupIsDone = false;

    @BeforeClass
    public static void setup() {

        bankingService = new BankingServiceImpl();
    }

    @Test // 1
    public void createCustomer() {
        String custName = "Rohan";
        String HomeAddressCity = "Pune";
        String HomeAddressState = "Maharashtra";
        int HomeAddressPincode = 411043;
        String LocalAddressCity = "Pune";
        String LocalAddressState = "Pune";
        int LocalAddressPincode = 411043;
        int day = 19;
        int month = 9;
        int year = 1998;

        int result = bankingService.acceptCustomerDetails(custName, HomeAddressCity, HomeAddressState,
                HomeAddressPincode, LocalAddressCity, LocalAddressState, LocalAddressPincode, day, month, year);
        System.out.println(bankingService.getLength());

        assertTrue(result > 0);

    }

    @Test // 2
    public void openCustomerAccount() {
        System.out.print(bankingService.getLength());
        int customerid = 100;
        int balance = 100000;
        String accountType = "savings";

        int result = bankingService.openAccount(customerid, balance, accountType);

        assertTrue(result > 0);

    }

    @Test // 3
    public void Balance() {
        int customerid = 100;
        int accountid = 50;
        int pin = 1007;

        int result = bankingService.getAccountBalance(customerid, accountid, pin);
        assertTrue(result > 0);
    }

    @Test // 4
    public void amt_withdraw() {
        int customerid = 100;
        int accountid = 50;
        int amount = 10000;
        int pin = 1007;

        int result = bankingService.withdraw(customerid, accountid, amount, pin);
        assertEquals(result, 90000);
    }

    @Test // 5
    public void transfer() {
        int customerid = 100;
        int accountid = 50;
        int customerid1 = 101;
        int accountid1 = 51;
        int amount = 10000;
        int pin = 1007;

        boolean result = bankingService.fundTransfer(customerid, accountid, customerid1, accountid1, amount, pin);
        assertEquals(result, true);
    }

    @Test // 6
    public void amt_deposit() {
        int customerid = 100;
        int accountid = 50;
        int amount = 10000;

        int result = bankingService.deposit(customerid, accountid, amount);
        assertEquals(result, 90000);
    }

    /*
     * @Test //7 public void cust_details() { int customerid = 100;
     * BankingServiceImpl bankingService = new BankingServiceImpl();
     * 
     * int result = bankingService.getCustomerDetails(customerid);
     * assertEquals(result, 90000); }
     */

    @Test // 10
    public void pin_change() {
        int customerid = 100;
        int accountid = 50;
        int o_pin = 1007;
        int n_pin = 1122;

        boolean result = bankingService.changePin(customerid, accountid, o_pin, n_pin);
        assertEquals(result, true);
    }

    @Test // 11
    public void check_change() {
        int customerid = 100;
        int accountid = 50;
        int pin = 1007;

        boolean result = bankingService.checkPin(customerid, accountid, pin);
        assertEquals(result, true);
    }
}

Service Class

package services;

import beans.Account;
import beans.Address;
import beans.Customer;
import beans.MyDate;
import beans.Transaction;
import dao.BankingSystemArrayImpl;

public class BankingServiceImpl {

    BankingSystemArrayImpl BankingSystemArray;

    public BankingServiceImpl() {
        System.out.print("called");
        BankingSystemArray = new BankingSystemArrayImpl();

    }

    /*
     * public void transfer(int accountId, int tansferAccountId, double amount)
     * { double a = BankingSystemArray.getAccount(accountId).getBalance()
     * - amount; System.out.println(a);
     * BankingSystemArray.getAccount(accountId).setBalance(a); double b =
     * BankingSystemArray.getAccount(accountId).getBalance() + amount;
     * BankingSystemArray.getAccount(tansferAccountId).setBalance(b);
     * 
     * }
     */

     public int acceptCustomerDetails(String custName, String HomeAddressCity,
            String HomeAddressState, int HomeAddressPincode,
            String LocalAddressCity, String LocalAddressState,
            int LocalAddressPincode, int day, int month, int year) {

        if ((day > 0 && day <= 31) && (month >= 1 && month <= 12)
                && (year <= 2015)) {
            return BankingSystemArray.insertCustomer(new Customer(
                    custName, new Address(LocalAddressCity, LocalAddressState,
                            LocalAddressPincode), new Address(HomeAddressCity,
                            HomeAddressState, HomeAddressPincode), new MyDate(
                            day, month, year)));
        } else
            return 0;

    }

    public int openAccount(int custId, int balance, String accType) {

        int accountId = 0;

        if (custId < 99) {
            System.out
                    .println("Invalid customer Id,please enter a valid customer Id");
        } else if (!(accType.equalsIgnoreCase("savings")
                || accType.equalsIgnoreCase("current") || accType
                    .equalsIgnoreCase("salary"))) {
            System.out
                    .println("Invalid account type, please enter a valid account type");
        } else if (balance < 0) {
            System.out.println("Invalid amount, please amount a valid amount");
        }

        else {
            Customer customer = BankingSystemArray.getCustomer(custId);

            if (customer == null) {
                System.out.println("Sorry you have not registered");
                return 0;
            } else {
                Account account = new Account(accType, balance);
                accountId = BankingSystemArray.insertAccount(account,
                        custId);
            }
        }

        return accountId;

    }

    public int getAccountBalance(int custId, int accNo, int pin) {

        if (checkPin(custId, accNo, pin)) {
            return BankingSystemArray.getAccount(custId, accNo)
                    .getBalance();
        } else {
            System.out.println("Invalid pin");
            return 0;
        }

    }

    public int withdraw(int custId, int accNo, int amt, int pin) {
        int balance = 0;
        if (amt < 0) {
            System.out.println("Invalid amount, please enter a valid amount");
        } else {

            Customer customer = BankingSystemArray.getCustomer(custId);

            if (customer == null) {
                return 0;
            } else {
                Account account = BankingSystemArray.getAccount(custId,
                        accNo);
                if (account == null) {
                    System.out.println("Sorry your account does not exist");
                } else if (account.getPin()!=pin) {
                    System.out.println("Invalid pin");
                    return 0;
                } else {
                    if ((account.getBalance() - amt) > 0) {
                        account.setBalance(account.getBalance() - amt);
                        balance = account.getBalance();
                    }
                }
            }
        }
        return balance;
    }

    public boolean fundTransfer(int custIdFrom, int accNoFrom, int custIdTo,
            int accNoTo, int amt, int pin) {
        if (withdraw(custIdFrom, accNoFrom, amt, pin) > 0) {
            deposit(custIdTo, accNoTo, amt);
            return true;
        }
        return false;
    }

    public int deposit(int custId, int accNo, int amt) {
        if (amt < 0) {
            System.out.println("Invalid amount, please enter a valid amount");
        } else {

            BankingSystemArray.getAccount(custId, accNo).setBalance(
                    BankingSystemArray.getAccount(custId, accNo)
                            .getBalance() + amt);

            return BankingSystemArray.getAccount(custId, accNo)
                    .getBalance();
        }
        return 0;
    }

    public Customer getCustomerDetails(int custId) {
        Customer customer = BankingSystemArray.getCustomer(custId);
        if (customer != null) {
            return customer;
        }
        return null;
    }

    public Account getAccountDetails(int custId, int accNo) {
        Account account = BankingSystemArray.getAccount(custId, accNo);
        if (account != null) {
            return account;
        }
        return null;
    }

    public Account[] getAllAccountsDetails(int custId) {
        Account[] account = BankingSystemArray.getAccountList(custId);
        if (account != null) {
            return account;
        }
        return null;
    }

    public Transaction[] getAllTransactionDetails(int custId, int accNo) {
        // TODO Auto-generated method stub
        return null;
    }

    public int generatePin(int custId, int accNo) {
        Account account = BankingSystemArray.getAccount(custId, accNo);
        int pin = BankingSystemArray.generateRandomNumber();
        account.setPin(pin);
        return account.getPin();
    }

    public boolean changePin(int custId, int accNo, int oldPin, int newPin) {
        Account account = BankingSystemArray.getAccount(custId, accNo);
        if (account != null) {
            if (account.getPin() == oldPin) {
                account.setPin(newPin);
                return true;
            }
        }

        return false;
    }

    public boolean checkPin(int custId, int accNo, int pin) {
        Account account = BankingSystemArray.getAccount(custId, accNo);
        if (account != null) {
            if (account.getPin() == pin) {
                return true;
            } 
        }
        return false;

    }

    public Customer getLength() {
        return BankingSystemArray.getLength();
    }

}

Aucun commentaire:

Enregistrer un commentaire