我有一个测试类,其中有我的Java项目的所有测试用例,我想一次初始化服务类,它创建一个数组,并且我想对所有其他测试用例使用相同的数组,这样做,但是当第一个测试用例运行时,将客户注册并存储在数组中,我想使用存储在数组中的客户并将其用于下一个测试用例,但看来客户不在数组中下一个测试用例,即正在创建一个新数组,或者我认为junit会分别运行每个测试用例
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);
}
}
服务等级
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();
}
}
答案 0 :(得分:0)
要解决您的问题,您首先需要了解JUnit行为。
@BeforeClass
和@AfterClass
的方法仅运行
一次,是您所有测试方法的上限和下限。@Before
和@After
的方法将立即运行
在任何测试方法之前和之后。还请注意,作为单元测试的规则,所有测试(所有测试方法)都必须相互隔离,但是在集成测试中可以。