本文从一个例子出发根据TDD(测试驱动开发)要求进行开发只是用于演示如何使用Spring提供的基于Annonation方式的IOC实现进行TDD开发
首先我们来看一下这个例子的要求开发一个购物车对象可以添加商品删除商品查询已购商口结账功能
第一步先来完成添加商品功能下面就按TDD开发要求先编写单元测试
下面是增对该功能编写的测试代码
/**
*@authorxmatthew
*
*/
@RunWith(SpringJUnitClassRunnerclass)
@ContextConfiguration(locations={classpath:/applicationContextxml})
@TestExecutionListeners({DependencyInjectionTestExecutionListenerclass})
publicclassCartTest{
@Autowired
privateSuperStoresuperStore;
@Test
publicvoidaddCommodity(){
Cartcart=newCart();
Commoditycommodity=superStoregetCommodity(/*电脑桌*/);
cartaddCommodity(commodity);
AssertassertEquals(cartsize());
AssertassertTrue(ntains(commodity));
}
}
当然这个单元测试不能通过(无法编译)接下来就是编写代码让单元测试能顺利通过添加 applicationContextxml文件
<?xmlversion=encoding=UTF?>
<beansxmlns=
xmlns:xsi=instance
xmlns:context=
xsi:schemaLocation=
beansxsd
contextxsd>
<context:componentscanbasepackage=comxmatthewspringtdd/>
<context:annotationconfig/>
</beans>
Commodityjava
/**
*@authorxmatthew
*
*/
publicclassCommodity{
privateStringid;
privateStringname;
privateBigDecimalprice;
/*(nonJavadoc)
*@seejavalangObject#equals(javalangObject)
*/
@Override
publicbooleanequals(finalObjectother){
if(!(otherinstanceofCommodity))
returnfalse;
CommoditycastOther=(Commodity)other;
returnnewEqualsBuilder()append(idcastOtherid)append(name
castOthername)append(pricecastOtherprice)isEquals();
}
/*(nonJavadoc)
*@seejavalangObject#hashCode()
*/
@Override
publicinthashCode(){
returnnewHashCodeBuilder()append(id)append(name)append(price)
toHashCode();
}
publicCommodity(StringidStringnameBigDecimalprice){
super();
thisid=id;
thisname=name;
thisprice=price;
}
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
thisid=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
thisname=name;
}
publicBigDecimalgetPrice(){
returnprice;
}
publicvoidsetPrice(BigDecimalprice){
thisprice=price;
}
}
SuperStorejava
/**
*@authorxmatthew
*
*/
publicinterfaceSuperStore{
CommoditygetCommodity(Stringid);
}
Cartjava
/**
*@authorxmatthew
*
*/
publicclassCart{
List<Commodity>commodities=newLinkedList<Commodity>();
publicvoidaddCommodity(Commoditycommodity){
commoditiesadd(commodity);
}
publicbooleancontains(Commoditycommodity){
//TODOAutogeneratedmethodstub
returnntains(commodity);
}
publicintsize(){
//TODOAutogeneratedmethodstub
returncommoditiessize();
}
}
/**
*@authorxmatthew
*
*/
@Service
publicclassWalMartimplementsSuperStore{
privateMap<StringCommodity>commodities;
publicWalMart(){
super();
commodities=newHashMap<StringCommodity>();
commoditiesput(newCommodity(电脑桌newBigDecimal()));
commoditiesput(newCommodity(电脑椅newBigDecimal()));
commoditiesput(newCommodity(电脑包newBigDecimal()));
}
publicCommoditygetCommodity(Stringid){
AsserthasText(ididisnull);
returncommoditiesget(id);
}
}
增加上面代码再运行单元测试测试通过TDD的基本开方法就类似下面几个步骤
* 编写测试代码
* 运行测试
* 完善(重构)代码
* 再测试
* 最终测试通过
示例比较简单只是为了演示基于Spring版本上如何进行TDD开发
我看到在Spring进行代码的测试变得非常简单
@RunWith(SpringJUnitClassRunnerclass)
@ContextConfiguration(locations={classpath:/applicationContextxml})
@TestExecutionListeners({DependencyInjectionTestExecutionListenerclass})
上面的代码就可实现自动加载Spring context
使用Spring的Configuration功能自动实现依赖注入感谢Spring Configuration项目让IOC实现更简单方便
当然不得不提的就是Junit 测试框架给是给我们编写单元简化了很多
本文示例代码下载点击