posts - 156,  comments - 601,  trackbacks - 0

 本文是针对 之前的通过Spring2.5对单元测试的Annotation支持进行TDD开发 进行扩展,增加了Struts Action层的测试实现。

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 }

实现比较简单,只需要注意以下两点:

1. 测试类必须继承 AnnotationStrutsSpringTest. 该代码在附件中
2. 通地
getProxyAction来构建Action类实例

这样虽然Action没有注入到Spring容器,也可以通过 getProxyAction方法,实现Spring容器的属性依赖注入实现。

源码下载: 下载

Good Luck!
Yours Matthew!




posted on 2011-02-27 19:15 x.matthew 阅读(1914) 评论(2)  编辑  收藏 所属分类: Spring|Hibernate|Other framework

只有注册用户登录后才能发表评论。


网站导航: