ええと、私はそれに他のマネーオブジェクトを追加できるマネーオブジェクトを持っています。 私はassertEquals()
をJavaで試してみましたが、私のコードが大丈夫ならばテストしましたが、失敗しました。assertEquals(obj、obj)は失敗したテストを返します
私のコードが正しいと確信しています(System.out.println
は正しい答えを返します)。私はちょうどassertEquals
を間違った方法で使用していると思います。 T_T
myObj1 == myObj2
がテストに使用されているかどうかを確認するにはどうすればよいですか?
**in my test.java**
assertEquals(new Money(money1.getCurrency(),new Value(22,70)),money1.add(money2));
**in my money class**
public class Money {
Currency currency;
Value value;
//constructor for Money class
public Money(Currency currency, Value value) {
super();
this.currency = currency;
this.value = value;
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
//must have same currency
public Money add(Money moneyToBeAdded){
Money result = new Money(moneyToBeAdded.currency, new Value(0,0));
Value totalInCents;
int tempCents;
int tempDollars;
if(compareCurrency(moneyToBeAdded)){
totalInCents = new Value(0,moneyToBeAdded.value.toCents()+value.toCents());
tempDollars = (totalInCents.toDollars().getDollar());
tempCents = (totalInCents.toDollars().getCents());
result = new Money(moneyToBeAdded.currency, new Value(tempDollars,tempCents));
System.out.println(result.value.getDollar()+"."+result.value.getCents());
}
return result;
}
private boolean compareCurrency(Money money){
return (money.currency.equals(currency))? true : false;
}
}
どこクラスマネーの 'equals'方法はありますか? – Paul
また、compareCurrencyでは、実質的に 'if(true)return true; else if(false)return false; '必要なものは以下の通りです:' return money.currency.equals(currency); ' – Paul
woops!これは欠落しているリンクでなければなりません.. equals()によるT__T –