您的位置 首页 >  博文

《项目架构那点儿事》——快速构建Junit用例

【前言】

按照惯例,在实际项目中我往往会对自己编写的程序进行测试,当测试通过后才能将其用于实战中,当然,编写单元测试是不可避免的,可以直接清晰的检验出 我们程序的可靠性、可只执行性,从中发现问题从而得到及时的解决,这里我就谈谈我们项目里Junit编写规范、模板,其中包括对web层、业务层的分布单 元测试。

【目录】

1Struts2Junit实现Web层单元测试
2SpringJunit实现业务层单元测试

【内容】

一、编写struts2Junit(依赖包:struts2-junit-plugin-2.1.8.jar,junit4,xwork-core- 2.2.1.jar,struts2-core-2.2.3.jar,版本号可以任意struts2版本),我以User为例子,下面是UserWebJunit:

 1 /**
2  * @author fisher
3  * @description struts2 单元测试用例模板
4  */

5public class Struts2JunitTemplate extends StrutsTestCase {
6
7    /**
8     * @description 测试ActionMapping,验证资源是否请求
9     */

10    @Test
11    public void testGetActionMapping() {
12        ActionMapping mapping = getActionMapping("/test/testAction.action");
13        assertNotNull(mapping);
14        assertEquals("/test", mapping.getNamespace());//验证命名空间
15        assertEquals("testAction", mapping.getName());//验证Action名称是否对应
16    }
17
18    /**
19     * @description 创建Action代理,验证请求参数、页面跳转
20     * @throws Exception
21     */

22    @Test
23    public void testGetActionProxy() throws Exception {
24        // 在执行Action方法之前,对request进行请求参数设置
25        request.setParameter("name""fisher");
26
27        ActionProxy proxy = getActionProxy("/test/testAction.action");
28        assertNotNull(proxy);
29        proxy.setExecuteResult(false);
30
31        @SuppressWarnings("rawtypes")
32        UserAction action = (UserAction ) proxy.getAction();//通过ActionProxy获得UserAction实例
33        assertNotNull(action);
34
35        String result = proxy.execute();//执行execute方法,返回结果
36        assertEquals(Action.SUCCESS, result);//比对返回结果是否和UserAction中的执行结果一致
37    }
38
39}

  二、编写SpringJunit(依赖包:spring-test-3.0.4.RELEASE.jar,junit4,以及其他的spring核心包),还是以User为例子,我们编写UserTestUnit来验证我们后台的方法,如下:

 1/**
2 * @author fisher
3 * @description 用户业务测试
4 */

5// 使用springJunit4
6@RunWith(SpringJUnit4ClassRunner.class)
7// spring配置文件加载(locations为文件路径)
8@ContextConfiguration(locations = {
9                "classpath:spring/application-hibernate.xml",
10                "classpath:spring/application-common-service.xml",
11                "classpath:spring/application-sys-service.xml" })
12public class UserTestJunit {
13    @Autowired
14    UserService userService;// 自动注入userService
15
16    /**
17     * @description 测试查询用户
18     * @throws Exception
19     */

20    @Test
21    public void query() throws Exception {
22        List result = userService.getAllEmployee();
23        Assert.notEmpty(result);
24    }
25
26    /**
27     * @description 测试用户添加
28     * @throws Exception
29     */

30
31    @Test
32    public void save() throws Exception {
33        User user = new User();
34        user.setUsrCode("test001");
35        user.setUsrName("test");
36        user.setPassword("123");
37        user.setIdCard("513029198503140026");
38        user.setEmail("aaa@sina.com");
39        User u = userService.save(user);
40        Assert.notNull(u);
41        org.junit.Assert.assertEquals("test", user.getUsrName());
42    }
43
44    /**
45     * @description 测试用户更新
46     * @throws Exception
47     */

48
49    @Test
50    public void update() throws Exception {
51        User user = new User();
52        user.setUsrCode("test001");
53        user.setUsrName("test");
54        user.setPassword("123");
55        user.setIdCard("513029198503140026");
56        user.setEmail("aaa@sina.com");
57        User u = userService.update(user);
58        Assert.notNull(u);
59        org.junit.Assert.assertEquals("test", user.getUsrName());
60    }
61
62    /**
63     * @description 测试用户删除
64     * @throws Exception
65     */

66
67    @Test
68    public void del() throws Exception {
69        User user = new User();
70        user.setUserId("1");
71        User u = userService.delete(user);
72        Assert.notNull(u);
73        org.junit.Assert.assertEquals("1", user.getUserId());
74    }
75}   

【总结】

单元测试不仅限于此,灵活性比较大,要结合实际进行编写,上面两种测试是按照我们项目中规范编写,大家可以作为参考,自我觉得还是比较实用而且用注解方式比较方便。


关于作者: 王俊南(Jonas)

昨夜寒蛩不住鸣。惊回千里梦,已三更。起来独自绕阶行。人悄悄,帘外月胧明。 白首为功名。旧山松竹老,阻归程。欲将心事付瑶琴。知音少,弦断有谁听。

热门文章