【发布时间】:2016-09-07 22:00:04
【问题描述】:
现在我正在阅读 Kent Beck 的“测试驱动开发”,但对“隐私”一章的理解存在问题。我使用 JUnit 4,这里是章节中的代码。
我们将测试的类:
public class Dollar {
private int amount;
Dollar(int amount) {
this.amount = amount;
}
public Dollar times(int multiplier) {
return new Dollar(amount * multiplier);
}
public boolean equals(Dollar d) {
return amount == d.amount;
}
}
测试(完全从书中复制):
public void testMultiplication() {
Dollar five= new Dollar(5);
assertEquals(new Dollar(10), five.times(2));
assertEquals(new Dollar(15), five.times(3));
}
阅读一章我知道这个测试应该有效。但它不能工作,因为times 方法总是返回新对象,所以assertEqualss 总是会失败。
我的问题是:这个测试应该有效吗?或者它只是作为将其翻译成另一种编程语言的示例?
【问题讨论】:
-
这种错误正是创建
@Override注解的原因。