如何为BigDecimal编写Junit测试用例

时间:2017-06-01 23:01:11

标签: java unit-testing java-ee junit junit4

你好我试着构建一个简单的银行应用程序现在我想写一个测试用例提取存款和转移请帮我,这里是代码

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);
        } 

    }

1 个答案:

答案 0 :(得分:1)

deposit方法不是设置余额,它是添加到现有余额。所以帐户最好还有余额。至少,您可以在balance字段的声明中使用初始值设定项:

class Account {
    ...
    private BigDecimal balance = BigDecimal.valueOf(0);
    ...
}

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