posts - 156,  comments - 601,  trackbacks - 0

           本文是針對 之前的通過Spring2.5對單元測試的Annotation支持進行TDD開發(fā) 進行擴展,增加了Struts Action層的測試實現(xiàn)。

          Action的測試代碼編寫如下:以WalMartAction為例
           1 public class WalMartAction {
           2 
           3     private SuperStore superStore;
           4 
           5     @Override
           6     public String toString() {
           7         return new ToStringBuilder(this).append("superStore", superStore)
           8                 .toString();
           9     }
          10 
          11     /**
          12      * @param superStore the superStore to set
          13      */
          14     public void setSuperStore(SuperStore superStore) {
          15         this.superStore = superStore;
          16     }
          17     
          18     public String list() {
          19         Collection<Commodity> commodities = superStore.getCommodities();
          20         System.out.println(commodities);
          21         
          22         return "SUCCESS";
          23     }
          24     
          25 }

          針對該Action編寫的測試代碼如下:
           1 @RunWith(SpringJUnit4ClassRunner.class)
           2 @ContextConfiguration(locations = {"classpath:/applicationContext-test.xml"})
           3 @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
           4 public class WalMartActionTest extends AnnotationStrutsSpringTest {
           5     
           6     private WalMartAction testAction;
           7 
           8     @Before
           9     public void setUp() {
          10         testAction = getProxyAction(WalMartAction.class);
          11         Assert.assertNotNull("TestAction should not null", testAction);
          12     }
          13     
          14     @After
          15     public void tearDown() {
          16         testAction = null;
          17     }
          18     
          19     @Test
          20     public void executeTestActionList() {
          21         System.out.println(testAction.list());
          22         
          23     }
          24     
          25 }

          實現(xiàn)比較簡單,只需要注意以下兩點:

          1. 測試類必須繼承 AnnotationStrutsSpringTest. 該代碼在附件中
          2. 通地
          getProxyAction來構(gòu)建Action類實例

          這樣雖然Action沒有注入到Spring容器,也可以通過 getProxyAction方法,實現(xiàn)Spring容器的屬性依賴注入實現(xiàn)。

          源碼下載: 下載

          Good Luck!
          Yours Matthew!




          posted on 2011-02-27 19:15 x.matthew 閱讀(1960) 評論(2)  編輯  收藏 所屬分類: Spring|Hibernate|Other framework
          主站蜘蛛池模板: 称多县| 石家庄市| 临安市| 柳州市| 台北市| 合作市| 武城县| 颍上县| 璧山县| 铜山县| 盐源县| 玉树县| 大同县| 新巴尔虎左旗| 抚顺市| 涪陵区| 和硕县| 通渭县| 长子县| 鄯善县| 高邮市| 开原市| 中卫市| 蓝山县| 随州市| 祁门县| 五台县| 曲阳县| 巴马| 莲花县| 乌拉特中旗| 凌源市| 澄迈县| 镇宁| 九江市| 江津市| 陕西省| 徐水县| 鹤壁市| 天峻县| 巴楚县|