zoukankan      html  css  js  c++  java
  • 《项目架构那点儿事》——快速构建Junit用例

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

    【目录】
              -----1.Struts2Junit实现Web层单元测试
              -----2.SpringJunit实现业务层单元测试

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

        /**
        * @author fisher
        * @description struts2 单元测试用例模板
        */
    
        public class Struts2JunitTemplate extends StrutsTestCase {
    
                /**
                 * @description 测试ActionMapping,验证资源是否请求
                 */
                @Test
                public void testGetActionMapping() {
                        ActionMapping mapping = getActionMapping("/test/testAction.action");
                        assertNotNull(mapping);
                        assertEquals("/test", mapping.getNamespace());//验证命名空间
                        assertEquals("testAction", mapping.getName());//验证Action名称是否对应
                }
    
                /**
                 * @description 创建Action代理,验证请求参数、页面跳转
                 * @throws Exception
                 */
                @Test
                public void testGetActionProxy() throws Exception {
                        // 在执行Action方法之前,对request进行请求参数设置
                        request.setParameter("name", "fisher");
    
                        ActionProxy proxy = getActionProxy("/test/testAction.action");
                        assertNotNull(proxy);
                        proxy.setExecuteResult(false);
                        
                        @SuppressWarnings("rawtypes")
                        UserAction action = (UserAction ) proxy.getAction();//通过ActionProxy获得UserAction实例
                        assertNotNull(action);
                        
                        String result = proxy.execute();//执行execute方法,返回结果
                        assertEquals(Action.SUCCESS, result);//比对返回结果是否和UserAction中的执行结果一致
                }
    
    
        }

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

        /**
        * @author fisher
        * @description 用户业务测试
        */
        // 使用springJunit4
        @RunWith(SpringJUnit4ClassRunner.class)
        // spring配置文件加载(locations为文件路径)
        @ContextConfiguration(locations = {
                        "classpath:spring/application-hibernate.xml",
                        "classpath:spring/application-common-service.xml",
                        "classpath:spring/application-sys-service.xml" })
        public class UserTestJunit {
                @Autowired
                UserService userService;// 自动注入userService
    
                /**
                 * @description 测试查询用户
                 * @throws Exception
                 */
                @Test
                public void query() throws Exception {
                        List result = userService.getAllEmployee();
                        Assert.notEmpty(result);
                }
    
                /**
                 * @description 测试用户添加
                 * @throws Exception
                 */
    
                @Test
                public void save() throws Exception {
                        User user = new User();
                        user.setUsrCode("test001");
                        user.setUsrName("test");
                        user.setPassword("123");
                        user.setIdCard("513029198503140026");
                        user.setEmail("aaa@sina.com");
                        User u = userService.save(user);
                        Assert.notNull(u);
                        org.junit.Assert.assertEquals("test", user.getUsrName());
                }
    
                /**
                 * @description 测试用户更新
                 * @throws Exception
                 */
    
                @Test
                public void update() throws Exception {
                        User user = new User();
                        user.setUsrCode("test001");
                        user.setUsrName("test");
                        user.setPassword("123");
                        user.setIdCard("513029198503140026");
                        user.setEmail("aaa@sina.com");
                        User u = userService.update(user);
                        Assert.notNull(u);
                        org.junit.Assert.assertEquals("test", user.getUsrName());
                }
    
                /**
                 * @description 测试用户删除
                 * @throws Exception
                 */
    
                @Test
                public void del() throws Exception {
                        User user = new User();
                        user.setUserId("1");
                        User u = userService.delete(user);
                        Assert.notNull(u);
                        org.junit.Assert.assertEquals("1", user.getUserId());
                }
        }

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

  • 相关阅读:
    SQL Server Management Studio
    uiimage拉伸
    时间空间复杂度
    冒泡选择排序

    插入排序
    快速构建APP
    TTTAtibutedlabel
    Git命令
    适配
  • 原文地址:https://www.cnblogs.com/xinhudong/p/4913484.html
Copyright © 2011-2022 走看看