本文從一個例子出發根據TDD(測試驅動開發)要求進行開發只是用於演示如何使用Spring提供的基於Annonation方式的IOC實現進行TDD開發
首先我們來看一下這個例子的要求開發一個購物車對象可以添加商品刪除商品查詢已購商口結賬功能
第一步先來完成添加商品功能下面就按TDD開發要求先編寫單元測試
下面是增對該功能編寫的測試代碼
/**
* @author xmatthew
*
*/
@RunWith(SpringJUnitClassRunnerclass)
@ContextConfiguration(locations = {classpath:/applicationContextxml})
@TestExecutionListeners({DependencyInjectionTestExecutionListenerclass})
public class CartTest {
@Autowired
private SuperStore superStore;
@Test
public void addCommodity() {
Cart cart = new Cart();
Commodity commodity = superStoregetCommodity(/*電腦桌*/);
cartaddCommodity(commodity);
AssertassertEquals( cartsize());
AssertassertTrue(ntains(commodity));
}
}
當然這個單元測試不能通過(無法編譯)接下來就是編寫代碼讓單元測試能順利通過添加 applicationContextxml文件
<?xml version= encoding=UTF?>
<beans xmlns=
xmlns:xsi=instance
xmlns:context=
xsi:schemaLocation=
beansxsd
contextxsd>
<context:componentscan basepackage=comxmatthewspringtdd/>
<context:annotationconfig/>
</beans>
Commodityjava
/**
* @author xmatthew
*
*/
public class Commodity {
private String id;
private String name;
private BigDecimal price;
/* (nonJavadoc)
* @see javalangObject#equals(javalangObject)
*/
@Override
public boolean equals(final Object other) {
if (!(other instanceof Commodity))
return false;
Commodity castOther = (Commodity) other;
return new EqualsBuilder()append(id castOtherid)append(name
castOthername)append(price castOtherprice)isEquals();
}
/* (nonJavadoc)
* @see javalangObject#hashCode()
*/
@Override
public int hashCode() {
return new HashCodeBuilder()append(id)append(name)append(price)
toHashCode();
}
public Commodity(String id String name BigDecimal price) {
super();
thisid = id;
thisname = name;
thisprice = price;
}
public String getId() {
return id;
}
public void setId(String id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
thisprice = price;
}
}
SuperStorejava
/**
* @author xmatthew
*
*/
public interface SuperStore {
Commodity getCommodity(String id);
}
Cartjava
/**
* @author xmatthew
*
*/
public class Cart {
List<Commodity> commodities = new LinkedList<Commodity>();
public void addCommodity(Commodity commodity) {
commoditiesadd(commodity);
}
public boolean contains(Commodity commodity) {
// TODO Autogenerated method stub
return ntains(commodity);
}
public int size() {
// TODO Autogenerated method stub
return commoditiessize();
}
}
/**
* @author xmatthew
*
*/
@Service
public class WalMart implements SuperStore {
private Map<String
Commodity> commodities;
public WalMart() {
super();
commodities = new HashMap<String
Commodity>(
);
commodities
put(
new Commodity(
電腦桌
new BigDecimal(
)));
commodities
put(
new Commodity(
電腦椅
new BigDecimal(
)));
commodities
put(
new Commodity(
電腦包
new BigDecimal(
)));
}
public Commodity getCommodity(String id) {
Assert
hasText(id
id is null
);
return commodities
get(id);
}
}
增加上面代碼再運行單元測試測試通過TDD的基本開方法就類似下面幾個步驟
* 編寫測試代碼
* 運行測試
* 完善(重構)代碼
* 再測試
* 最終測試通過
示例比較簡單只是為了演示基於Spring版本上如何進行TDD開發
我看到在Spring進行代碼的測試變得非常簡單
@RunWith(SpringJUnitClassRunnerclass)
@ContextConfiguration(locations = {classpath:/applicationContextxml})
@TestExecutionListeners({DependencyInjectionTestExecutionListenerclass})
上面的代碼就可實現自動加載Spring context
使用Spring的Configuration功能自動實現依賴注入感謝Spring Configuration項目讓IOC實現更簡單方便
當然不得不提的就是Junit 測試框架給是給我們編寫單元簡化了很多
本文示例代碼下載點擊
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28807.html