测试驱动开发(第二节)

对于那些可以被当做数值来使用的对象,我们称为VO(Value,Object)数值对象,数值对象的一个要求是一旦数值对象的实例变量值在构造函数中被指定,那么以后就再也不允许变化。
数值对象的一个隐含意思就是,所有的操作都必须返回一个对象,另一个隐含意思就是使用数值对象必须要实现equals函数
//Dollor实体类:
package com.hellokitty.pro;

public class Dollor {
public int ammount;
    //构造函数
public Dollor(int amount) {
this.ammount = amount;
}
public Dollor times(int multiplier) {
return new Dollor(this.ammount*multiplier);
}
public boolean equals(Object object) {
Dollor dollor = (Dollor)object;
return ammount == dollor.ammount;
}
}
//测试类:
package com.hellokitty.tdd;

import org.junit.Test;

import junit.framework.Assert;

import com.hellokitty.pro.Dollor;

public class TestDollor {
@Test
public void testMultiPlication() throws Exception {
Dollor five = new Dollor(5);
Dollor six = new Dollor(6);
Dollor product = five.times(2);
Assert.assertEquals(10, product.ammount);
product = five.times(3);
Assert.assertEquals(15, product.ammount);
Assert.assertTrue(new Dollor(5).equals(new Dollor(5)));
}
}

猜你喜欢

转载自happyforever.iteye.com/blog/1470063