关键字: TDD, Junit, Spring, Test
本文从一个例子出发,根据TDD(测试驱动开发)要求,进行开发。只是用于演示如何使用Spring2.5提供的基于Annonation方式的IOC实现,进行TDD开发。
首先我们来看一下这个例子的要求:
开发一个购物车对象,可以添加商品,删除商品,查询已购商口,结账功能。
第一步,先来完成添加商品功能,下面就按TDD开发要求,先编写单元测试:
下面是增对该功能,编写的测试代码
1 /**
2 * @author xmatthew
3 *
4 */
5 @RunWith(SpringJUnit4ClassRunner.class)
6 @ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
7 @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
8 public class CartTest {
9
10 @Autowired
11 private SuperStore superStore;
12
13 @Test
14 public void addCommodity() {
15
16 Cart cart = new Cart();
17 Commodity commodity = superStore.getCommodity("1"/*电脑桌*/);
18 cart.addCommodity(commodity);
19
20 Assert.assertEquals(1, cart.size());
21 Assert.assertTrue(cart.contains(commodity));
22
23 }
24 }
当然这个单元测试不能通过(无法编译).接下来就是编写代码,让单元测试能顺利通过
添加 applicationContext.xml文件
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context-2.5.xsd">
9
10
11 <context:component-scan base-package="com.xmatthew.spring.tdd"/>
12 <context:annotation-config/>
13
14
15 </beans>
16
Commodity.java
1 /**
2 * @author xmatthew
3 *
4 */
5 public class Commodity {
6
7 private String id;
8 private String name;
9 private BigDecimal price;
10
11 /* (non-Javadoc)
12 * @see java.lang.Object#equals(java.lang.Object)
13 */
14 @Override
15 public boolean equals(final Object other) {
16 if (!(other instanceof Commodity))
17 return false;
18 Commodity castOther = (Commodity) other;
19 return new EqualsBuilder().append(id, castOther.id).append(name,
20 castOther.name).append(price, castOther.price).isEquals();
21 }
22
23 /* (non-Javadoc)
24 * @see java.lang.Object#hashCode()
25 */
26 @Override
27 public int hashCode() {
28 return new HashCodeBuilder().append(id).append(name).append(price)
29 .toHashCode();
30 }
31
32 public Commodity(String id, String name, BigDecimal price) {
33 super();
34 this.id = id;
35 this.name = name;
36 this.price = price;
37 }
38
39 public String getId() {
40 return id;
41 }
42
43 public void setId(String id) {
44 this.id = id;
45 }
46
47 public String getName() {
48 return name;
49 }
50
51 public void setName(String name) {
52 this.name = name;
53 }
54
55 public BigDecimal getPrice() {
56 return price;
57 }
58
59 public void setPrice(BigDecimal price) {
60 this.price = price;
61 }
62
63
64 }
65
SuperStore.java
1 /**
2 * @author xmatthew
3 *
4 */
5 public interface SuperStore {
6
7
8 Commodity getCommodity(String id);
9 }
Cart.java
1 /**
2 * @author xmatthew
3 *
4 */
5 public class Cart {
6
7 List<Commodity> commodities = new LinkedList<Commodity>();
8
9 public void addCommodity(Commodity commodity) {
10 commodities.add(commodity);
11
12 }
13
14 public boolean contains(Commodity commodity) {
15 // TODO Auto-generated method stub
16 return commodities.contains(commodity);
17 }
18
19 public int size() {
20 // TODO Auto-generated method stub
21 return commodities.size();
22 }
23
24 }
1 /**
2 * @author xmatthew
3 *
4 */
5 @Service
6 public class WalMart implements SuperStore {
7
8 private Map<String, Commodity> commodities;
9
10 public WalMart() {
11 super();
12 commodities = new HashMap<String, Commodity>(3);
13 commodities.put("1", new Commodity("1", "电脑桌", new BigDecimal(1288)));
14 commodities.put("2", new Commodity("2", "电脑椅", new BigDecimal(588)));
15 commodities.put("3", new Commodity("1", "电脑包", new BigDecimal(368)));
16 }
17
18 public Commodity getCommodity(String id) {
19 Assert.hasText(id, "id is null");
20 return commodities.get(id);
21 }
22
23 }
增加上面代码,再运行单元测试,测试通过。TDD的基本开方法就类似下面几个步骤:
* 编写测试代码
* 运行测试
* 完善(重构)代码
* 再测试
* 最终测试通过
示例比较简单,只是为了演示基于Spring2.5版本上如何进行TDD开发。
我看到在Spring2.5进行代码的测试变得非常简单
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
上面的代码,就可实现自动加载Spring context
@Autowired
private SuperStore superStore;
使用Spring的Configuration功能,自动实现依赖注入。感谢Spring Configuration项目,让IOC实现更简单,方便。
当然不得不提的就是Junit4 测试框架给是给我们编写单元简化了很多。
本文示例代码
下载
Good Luck!
Yours Matthew!