<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 156,  comments - 601,  trackbacks - 0
    關鍵字: TDD, Junit, Spring, Test

    本文從一個例子出發(fā),根據(jù)TDD(測試驅動開發(fā))要求,進行開發(fā)。只是用于演示如何使用Spring2.5提供的基于Annonation方式的IOC實現(xiàn),進行TDD開發(fā)。

    首先我們來看一下這個例子的要求:
      開發(fā)一個購物車對象,可以添加商品,刪除商品,查詢已購商口,結賬功能。

    第一步,先來完成添加商品功能,下面就按TDD開發(fā)要求,先編寫單元測試:

    下面是增對該功能,編寫的測試代碼
     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開發(fā)。

    我看到在Spring2.5進行代碼的測試變得非常簡單
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations 
    = {"classpath:/applicationContext.xml"})
    @TestExecutionListeners({DependencyInjectionTestExecutionListener.
    class})
    上面的代碼,就可實現(xiàn)自動加載Spring context

    @Autowired
    private SuperStore superStore;
    使用Spring的Configuration功能,自動實現(xiàn)依賴注入。感謝Spring Configuration項目,讓IOC實現(xiàn)更簡單,方便。
     
    當然不得不提的就是Junit4 測試框架給是給我們編寫單元簡化了很多。
    本文示例代碼下載

    Good Luck!
    Yours Matthew!


    posted on 2008-11-05 19:40 x.matthew 閱讀(1917) 評論(2)  編輯  收藏 所屬分類: Spring|Hibernate|Other framework
    主站蜘蛛池模板: 亚洲αⅴ无码乱码在线观看性色 | 中国一级毛片视频免费看| 一级做a爰性色毛片免费| 精品一区二区三区免费视频| 国产白丝无码免费视频| 国产成人yy免费视频| 免费无码看av的网站| 久久激情亚洲精品无码?V| 亚洲精品视频在线免费| 亚洲AV日韩AV无码污污网站| 国产免费黄色无码视频| MM131亚洲国产美女久久 | 亚洲综合色7777情网站777| 国产亚洲人成在线影院| 精品熟女少妇av免费久久| 国产一级一片免费播放i| 无码欧精品亚洲日韩一区| 亚洲av日韩av永久在线观看 | 亚洲精品色播一区二区| 免费永久看黄在线观看app| 一级免费黄色大片| 亚洲AV乱码久久精品蜜桃| 黄色网址免费大全| 亚洲av乱码中文一区二区三区| 亚洲免费在线观看| 亚洲午夜无码久久久久软件| 久久久久国产精品免费免费不卡| 免费在线观看理论片| 亚洲中文字幕无码久久| 免费人成在线观看播放国产| 国产一级婬片A视频免费观看| 中文字幕亚洲色图| xvideos永久免费入口| 亚洲丝袜美腿视频| 狠狠躁狠狠爱免费视频无码| 亚洲尹人九九大色香蕉网站| 成全视频在线观看免费| 亚洲中文字幕伊人久久无码| 99精品视频免费在线观看| 亚洲AV无码第一区二区三区| 久久国产精品免费一区二区三区|