OOCamp--测试驱动开发
现在有类似这样一个需求:需要提供一个简单类库,以供其他开发者调用。现在进行Tasking,最简单的需求,这个类中应该拥有一个value记录长度值,也应该有一个单位unit来记录相应的单位,对于一个length对象来说,用户只关心我拿到这个对象后怎么用,比如,我两个对象可以比较是否相等,是否可以相加,对于其length的value和unit来说,也许用户并不关心他们的行为(至少现在是这样的),所以完全没必要为也不应该其提供相应的getter/setter 方法。现在我们来实现两个Length对象比较是否相等的行为。
下面的测试用例我们很容易想到:
1. 1m = 1m
2. 1m = 100cm
3. 1m != 2m
4. 1m = 1000mm
接着我们就需要开始写测试代码了:
import org.junit.Test;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;public class LengthTest { @Test public void should_1_m_equals_1_m(){ Length length1 = new Length(1,"m"); Length length2 = new Length(1,"m"); assertThat(length1.equals(length2),is(true)); }}
public class Length { private int value; private String unit; public Length(int value, String unit) { this.value = value; this.unit = unit; } @Override public boolean equals(Object obj){ Length anotherLength = (Length) obj; return (this.unit.equals(anotherLength.unit)&&this.value == anotherLength.value); }}
@Test public void should_1m_equals_100_cm(){ Length length1 = new Length(1,"m"); Length length2 = new Length(100,"cm"); assertThat(length1.equals(length2),is(true)); }
@Override public boolean equals(Object obj){ Length anotherLength = (Length) obj; if(anotherLength.unit.equals("cm")){ return this.value * 100 == anotherLength.value; } return (this.unit.equals(anotherLength.unit)&&this.value == anotherLength.value); }
package com.lee.oocamp.blog;import org.junit.Test;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;public class LengthTest { @Test public void should_1_m_equals_1_m(){ Length length1 = new Length(1,"m"); Length length2 = new Length(1,"m"); assertThat(length1.equals(length2),is(true)); } @Test public void should_1_m_equals_100_cm(){ Length length1 = new Length(1,"m"); Length length2 = new Length(100,"cm"); assertThat(length1.equals(length2),is(true)); } @Test public void should_1_m_not_equal_2_m(){ Length length1 = new Length(1,"m"); Length length2 = new Length(2,"m"); assertThat(length1.equals(length2),is(false)); } @Test public void should_1_m_equals_1000_mm(){ Length length1 = new Length(1,"m"); Length length2 = new Length(1000,"mm"); assertThat(length1.equals(length2),is(true)); }}
@Override public boolean equals(Object obj){ Length anotherLength = (Length) obj; if(anotherLength.unit.equals("cm")){ return this.value * 100 == anotherLength.value; }else if(anotherLength.unit.equals("mm")){ return this.value * 1000 == anotherLength.value; }else{ return this.value*1 == anotherLength.value; } }
@Test public void should_1000_mm_equals_1_m(){ Length length1 = new Length(1000,"mm"); Length length2 = new Length(1,"m"); assertThat(length1.equals(length2),is(true)); } @Test public void should_100_cm_equals_1_m(){ Length length1 = new Length(100,"cm"); Length length2 = new Length(1,"m"); assertThat(length1.equals(length2),is(true)); }
public class Length { private int value; private String unit; public Length(int value, String unit) { this.value = getValue(unit,value); this.unit = unit; } private int getValue(String unit, int value) { int result = 0; if(unit.equals("m")){ result = value * 1000; }else if(unit.equals("cm")){ result = value * 10; }else if(unit.equals("mm")){ result = value * 1; } return result; } @Override public boolean equals(Object obj){ Length anotherLength = (Length) obj; return this.value == anotherLength.value; }}
Length类:public class Length { private int value; private Length() { } public static Length createLength(int value, UNIT unit) { Length length = new Length(); length.value = unit.getTheValue(value); return length; } @Override public boolean equals(Object obj){ boolean result = false; Length anotherLength = (Length) obj; result = this.value == anotherLength.value; return result; }}
public enum UNIT { M(1000),CM(10),MM(1); int radio; UNIT(int radio){ this.radio = radio; } public int getTheValue(int value) { int result = value * this.radio; return result; }}
import org.junit.Test;import com.lee.oocamp.Length;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;public class LengthTest { @Test public void should_1_m_equals_1_m(){ compareTwoLengthObj(1,UNIT.M,1,UNIT.M,true); } @Test public void should_1_m_not_equal_2m(){ compareTwoLengthObj(1,UNIT.M,2,UNIT.M,false); } @Test public void should_1_m_not_equal_1cm(){ compareTwoLengthObj(1,UNIT.M,1,UNIT.CM,false); } @Test public void should_1_m_equals_100cm(){ compareTwoLengthObj(1, UNIT.M, 100, UNIT.CM ,true); } @Test public void should_1_m_equals_1000mm(){ compareTwoLengthObj(1, UNIT.M, 1000, UNIT.MM ,true); } @Test public void should_100_cm_equals_1m(){ compareTwoLengthObj(100, UNIT.CM, 1, UNIT.M ,true); } @Test public void should_1000_mm_equals_1m(){ compareTwoLengthObj(1000, UNIT.MM, 1, UNIT.M ,true); } @Test public void should_2000_mm_not_equals_1m(){ compareTwoLengthObj(2000, UNIT.MM, 1, UNIT.M ,false); } private void compareTwoLengthObj(int valueOfLength1,UNIT unitOfLength1,int valueOfLength2,UNIT unitOfLength2,boolean expect) { Length length1 = Length.createLength(valueOfLength1,unitOfLength1); Length length2 = Length.createLength(valueOfLength2,unitOfLength2); assertThat(length1.equals(length2),is(expect)); } }