通过Spring2.5对单元测试的Annotation支持进行TDD开发

本文通过一个购物车功能的例子,展示了如何使用Spring 2.5的注解方式实现IOC,并采用测试驱动开发(TDD)的方法逐步构建功能。首先编写了无法通过的单元测试,接着创建配置文件及商品类等,确保测试可以通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文从一个例子出发,根据TDD(测试驱动开发)要求,进行开发。只是用于演示如何使用Spring2.5提供的基于Annonation方式的IOC实现,进行TDD开发。

    首先我们来看一下这个例子的要求:开发一个购物车对象,可以添加商品,删除商品,查询已购商口,结账功能。

    第一步,先来完成添加商品功能,下面就按TDD开发要求,先编写单元测试

    下面是增对该功能,编写的测试代码

  /**
  * @author xmatthew
  *
  */
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
  @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
  public class CartTest {
  
     @Autowired
     private SuperStore superStore;
     
     @Test
     public void addCommodity() {
 
         Cart cart = new Cart();
         Commodity commodity = superStore.getCommodity("1"/*电脑桌*/);
         cart.addCommodity(commodity);
         
         Assert.assertEquals(1, cart.size());
         Assert.assertTrue(cart.contains(commodity));
         
     }
 }

 

 

 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文件

  

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
  
     <context:component-scan base-package="com.xmatthew.spring.tdd"/>
     <context:annotation-config/>
 
  
 </beans>
 

 

 

 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

 /**
  * @author xmatthew
  *
  */
 public class Commodity {
 
     private String id;
     private String name;
     private BigDecimal price;
     
     /* (non-Javadoc)
      * @see java.lang.Object#equals(java.lang.Object)
      */
     @Override
     public boolean equals(final Object other) {
         if (!(other instanceof Commodity))
             return false;
         Commodity castOther = (Commodity) other;
         return new EqualsBuilder().append(id, castOther.id).append(name,
                 castOther.name).append(price, castOther.price).isEquals();
     }
 
     /* (non-Javadoc)
      * @see java.lang.Object#hashCode()
      */
     @Override
     public int hashCode() {
         return new HashCodeBuilder().append(id).append(name).append(price)
                 .toHashCode();
     }
 
     public Commodity(String id, String name, BigDecimal price) {
         super();
         this.id = id;
         this.name = name;
         this.price = price;
     }
 
     public String getId() {
         return id;
     }
 
     public void setId(String id) {
         this.id = id;
     }
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this.name = name;
     }
 
     public BigDecimal getPrice() {
         return price;
     }
 
     public void setPrice(BigDecimal price) {
         this.price = price;
     }
     
     
 }
 

 

 

 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

 /**
  * @author xmatthew
  *
  */
 public interface SuperStore {
 
     
     Commodity getCommodity(String id);
 }

 

 

1 /**
2  * @author xmatthew
3  *
4  */
5 public interface SuperStore {
6 
7     
8     Commodity getCommodity(String id);
9 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值