[英]how to write Junit test case for BigDecimal
您好,我現在嘗試構建一個簡單的銀行應用程序,我想編寫用於提取存款和轉賬的測試用例,請幫助我,這是代碼
Account.java
public BigInteger getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(BigInteger accountNumber) {
this.accountNumber = accountNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOwner() {
return ownerName;
}
public void setOwner(String owner) {
this.ownerName = owner;
}
public BigDecimal getBalance() {
return balance;
}
public BigDecimal setBalance(BigDecimal balance) {
this.balance = balance;
return balance;
}
Bank.java
package org.mybank.entities;
// Bank details.
public class Bank {
private String IFSC;
private Integer id;
private String address;
public String getIFSC() {
return IFSC;
}
public void setIFSC(String iFSC) {
IFSC = iFSC;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
chekings.java
package org.mybank.entities;
// checking account extending Account
public class Chekings extends Account {
private String checkingtype;
public String getCheckingtype() {
return checkingtype;
}
public void setCheckingtype(String checkingtype) {
this.checkingtype = checkingtype;
}
}
積蓄
package org.mybank.entities;
// Savings account extending Account
public class Savings extends Account {
private String savingsType;
public String getSavingsType() {
return savingsType;
}
public void setSavingsType(String savingsType) {
this.savingsType = savingsType;
}
}
transaction.java
package org.mybank.entities;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Transaction {
// main class
private BigDecimal amout;
private String transactionType;
private BigInteger sourceAccNum;
private BigInteger destAccNum;
private Integer transactionId;
}
這是Operations.java,IAM在其中執行測試用例以提取存款和轉移
package org.mybank.business;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.mybank.entities.Account;
import org.mybank.entities.Savings;
public class Operations {
// withdraw specified amount from the specified account number
public void withdrawl(BigInteger accNum, BigDecimal amount) throws Exception {
Account acc = findAccount(accNum);
if (acc!=null) { // check if it is valid account
if (acc.getBalance().floatValue() >= amount.floatValue()) {
if (acc.getType().equals("SAVINGS")) { // if savings account.
Savings savingsAccount = getSavingsAccount(acc.getAccountNumber());
if (savingsAccount.getSavingsType().equals("INDIVIDUAL") && amount.floatValue() > 1000) {
throw new Exception("Idividual account cannot withdraw amount more than 1000.");
} else {
// create a transaction here
acc.setBalance(new BigDecimal(acc.getBalance().floatValue() - amount.floatValue()));
}
}
} else {
throw new Exception("Insufficient funds");
}
}else{
throw new Exception("Invalid Account");
}
}
//returns saving acc based on account number
private Savings getSavingsAccount(BigInteger accountNumber) {
return new Savings();
}
// returns account based on account number
private Account findAccount(BigInteger accNum) {
// TODO Auto-generated method stub
return new Account();
}
// transfers funds from source to destination.
public void transfer(BigInteger sourceAccNum, BigInteger destAccNum, BigDecimal amount) throws Exception {
Account src = findAccount(sourceAccNum);
Account dest = findAccount(destAccNum);
if(src!=null && dest!=null){
if(src.getBalance().floatValue()>amount.floatValue()){
// create a new trascation instance here
src.setBalance(new BigDecimal(src.getBalance().floatValue() - amount.floatValue()));
dest.setBalance(new BigDecimal(dest.getBalance().floatValue() + amount.floatValue()));
}else{
throw new Exception("Insufficient funds");
}
}else{
throw new Exception("Invalid account.");
}
}
// deposit amount in specified account.
public BigDecimal deposit(BigInteger accNum, BigDecimal amount) throws Exception {
Account acc = findAccount(accNum);
if(acc!=null){
// create a new trascation instance here
acc.setBalance(new BigDecimal(acc.getBalance().floatValue() + amount.floatValue()));
}else{
throw new Exception("Invalid Account");
}
return amount;
}
}
這是我編寫的測試代碼。 它總是失敗。如果我使用try catch塊,它總是通過測試,幫助我編寫完美的測試代碼。 這是我第一次用Junit編寫測試代碼。
import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Test;
import org.mybank.business.Operations;
import junit.framework.Assert;
public class banking {
@Test
public void deposittest() throws Exception {
Operations optest = new Operations();
int a= 123;
Double b= (double) 2000;
BigDecimal result;
result = optest.deposit(BigInteger.valueOf(a),BigDecimal.valueOf(b));
assertEquals(2000,result);
}
}
deposit
方法不是設置余額,而是添加到現有余額中。 因此,帳戶最好已有一些余額。 至少,您可以在balance
字段的聲明中使用初始化程序:
class Account {
...
private BigDecimal balance = BigDecimal.valueOf(0);
...
}
您也可以將初始化作為構造函數的一部分來處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.