?????????????????????????????????????????????в????????????????д??????????????????????????????д????????д????????????Щ??????????????Щ?????????????????д??????????????????????????????? $1.23 ?? $2.8 ??????????ó? $4.03???????? $3.03 ?? $4.029999998?????????????? $7.465 ??????????????????????????β???? $7.50 ???? €5.88 ?????????????????????????????????????????????????£???????????г??????amount.toEuros() ???????????????б仯??
???????? mock ???????????????????????????????????μ???????????????????? mock ??????????????????????????????????????????????????????????в??????????????????? toEuros() ?????е????????????????????????????????????????????????????????????£??????? mock ??????????? fake??
????mock ?????????????????????????磬??? toEuros() ???????????μ??????????????ж??????????????????????????????????γ???????????в?????????д?????????????? mock ??????μ??
????mock ?????????????????????????????? mock ?????У???????????????????????????????????????????????Э???????????? mock ????????????в?????????????????????????????????????
??????mock ???????????????????????????????????????????漰 mock ?????????е????????????????????е?????????????????????е???????????????????????????
????EasyMock ???????? Java ?????????????? mock ????????????????????????????????Щ????? mock ????EasyMock ???????????????????д???????????κν???????????????? EasyMock ?????????????????? mock??????????κ??????????Щ mock???????????е????????????????з???????????ò????
????EasyMock ???
???????????????????????? EasyMock ???????????嵥 1 ???鹹?? ExchangeRate ???????κν????????????????????????????????????????????磬???????????? Yahoo ??????????????????????????????????
?????嵥 1. ExchangeRate
????import java.io.IOException;
????public interface ExchangeRate {
????double getRate(String inputCurrency?? String outputCurrency) throws IOException;
????}
?????嵥 2 ?????? Currency ???????????????????????????? bug?????????????????? bug????????в??????
?????嵥 2. Currency ??

 

import java.io.IOException;
public class Currency {
private String units;
private long amount;
private int cents;
public Currency(double amount?? String code) {
this.units = code;
setAmount(amount);
}
private void setAmount(double amount) {
this.amount = new Double(amount).longValue();
this.cents = (int) ((amount * 100.0) % 100);
}
public Currency toEuros(ExchangeRate converter) {
if ("EUR".equals(units)) return this;
else {
double input = amount + cents/100.0;
double rate;
try {
rate = converter.getRate(units?? "EUR");
double output = input * rate;
return new Currency(output?? "EUR");
} catch (IOException ex) {
return null;
}
}
}
public boolean equals(Object o) {
if (o instanceof Currency) {
Currency other = (Currency) o;
return this.units.equals(other.units)
&& this.amount == other.amount
&& this.cents == other.cents;
}
return false;
}
public String toString() {
return amount + "." + Math.abs(cents) + " " + units;
}
}