gpt4 book ai didi

java - 如何为 BigDecimal 编写 Junit 测试用例

转载 作者:行者123 更新时间:2023-12-02 13:00:34 25 4
gpt4 key购买 nike

您好,我尝试构建一个简单的银行应用程序,现在我想编写提款存款和转账的测试用例,请帮助我,这是代码

帐户.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;
}

银行.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;
}



}

交易.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 block ,它总是通过测试帮助我编写完美的测试代码。这是我第一次在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);
...
}

您还可以将初始化作为构造函数的一部分进行处理。

关于java - 如何为 BigDecimal 编写 Junit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44318355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com