public class Money {
private int fAmount;
private String fCurrency;
public Money(int amount,String currency) {
fAmount=amount;
fCurrency=currency;
}
public int amount()
{
return fAmount;
}
public String currency()
{
return fCurrency;
}
public Money add(Money m)
{
return new Money(amount()+m.amount(),currency());
}
public boolean equals(Object anObject)
{
if(anObject instanceof Money)
{
Money aMoney=(Money)anObject;
return aMoney.currency().equals(currency())
&& amount()==aMoney.amount();
}
return false;
}
}
该类有两个私有属性fAmount和fCurrency,构造函数在对类进行初始化的时候,对两个私有属性进行了赋值。Add方法对两个Money对象的现金(fAmount)进行相加,equals方法对两个Money对象的现金及货币单位进行比较是否相等。
4 创建单元测试
在项目窗口中的类Money.class上鼠标右键单击,在弹出的菜单中选择“工具”,并在子菜单中选择“创建Junit测试”,如下图所示:
文章来源于领测软件测试网 https://www.ltesting.net/