h*********e 发帖数: 247 | 1 import java.io.IOException;
public interface ExchangeRate {
double getRate(String inputCurrency, String outputCurrency) throws
IOException;
}
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;
}
}
import junit.framework.TestCase;
import org.easymock.EasyMock;
import java.io.IOException;
public class CurrencyTest extends TestCase {
public void testToEuros() throws IOException {
Currency testObject = new Currency(2.50, "USD");
Currency expected = new Currency(3.75, "EUR");
ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
EasyMock.replay(mock);
Currency actual = testObject.toEuros(mock);
assertEquals(expected, actual);
}
}
不管真正的ExchangeRate concrete class在不在, 有没有bug,
上面的Junit根本就没有测试真正的class/method, 其文档说验证方法的调用种类、次
数、顺序,可以令 Mock 对象返回指定的值或抛出指定异常。这我看到了, 但这种测
试到底价值何在? | y***d 发帖数: 2330 | 2 这个测试的是 Currency 的 toEuros,不是 ExchangeRate
【在 h*********e 的大作中提到】 : import java.io.IOException; : public interface ExchangeRate { : double getRate(String inputCurrency, String outputCurrency) throws : IOException; : } : import java.io.IOException; : public class Currency { : private String units; : private long amount; : private int cents;
| g*****g 发帖数: 34805 | 3 EasyMock does a few things. It mocks your object easily.
It can check both input and output of your method. And
it can verify the sequence of called methods.
【在 h*********e 的大作中提到】 : import java.io.IOException; : public interface ExchangeRate { : double getRate(String inputCurrency, String outputCurrency) throws : IOException; : } : import java.io.IOException; : public class Currency { : private String units; : private long amount; : private int cents;
| p******w 发帖数: 62 | 4 mock是把你不要测的东西mock出去,只是让他提供最基本的返回,在这个例子里面,这
个unit test主要是测toEuro里的逻辑,不用测exchangerate是否正确,所以mock了
exchangerate,让他返回一个你可以控制的值,从而让你判断toEuro是否做了应该做的
事情。
【在 h*********e 的大作中提到】 : import java.io.IOException; : public interface ExchangeRate { : double getRate(String inputCurrency, String outputCurrency) throws : IOException; : } : import java.io.IOException; : public class Currency { : private String units; : private long amount; : private int cents;
|
|