博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《项目架构那点儿事》——快速构建Junit用例
阅读量:4322 次
发布时间:2019-06-06

本文共 4552 字,大约阅读时间需要 15 分钟。

【前 言】按照惯例,在实际项目中我往往会对自己编写的程序进行测试,当测试通过后才能将其用于实战中,当然,编写单元测试是不可避免的,可以直接清晰的检验出 我们程序的可靠性、可只执行性,从中发现问题从而得到及时的解决,这里我就谈谈我们项目里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());            }    }

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

 

转载于:https://www.cnblogs.com/xinhudong/p/4913484.html

你可能感兴趣的文章
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>
Array.of使用实例
查看>>
【Luogu】P2498拯救小云公主(spfa)
查看>>
如何获取网站icon
查看>>
几种排序写法
查看>>
java 多线程的应用场景
查看>>
dell support
查看>>