<<项目架构那点儿事>>——快速构建Junit用例

欢迎访问我的社区资源论坛http://www.javadt.com

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

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

  5. public class Struts2JunitTemplate extends StrutsTestCase {

  6.         /**
  7.          * @description 测试ActionMapping,验证资源是否请求
  8.          */
  9.         @Test
  10.         public void testGetActionMapping() {
  11.                 ActionMapping mapping = getActionMapping("/test/testAction.action");
  12.                 assertNotNull(mapping);
  13.                 assertEquals("/test", mapping.getNamespace());//验证命名空间
  14.                 assertEquals("testAction", mapping.getName());//验证Action名称是否对应
  15.         }

  16.         /**
  17.          * @description 创建Action代理,验证请求参数、页面跳转
  18.          * @throws Exception
  19.          */
  20.         @Test
  21.         public void testGetActionProxy() throws Exception {
  22.                 // 在执行Action方法之前,对request进行请求参数设置
  23.                 request.setParameter("name", "fisher");

  24.                 ActionProxy proxy = getActionProxy("/test/testAction.action");
  25.                 assertNotNull(proxy);
  26.                 proxy.setExecuteResult(false);
  27.                 
  28.                 @SuppressWarnings("rawtypes")
  29.                 UserAction action = (UserAction ) proxy.getAction();//通过ActionProxy获得UserAction实例
  30.                 assertNotNull(action);
  31.                 
  32.                 String result = proxy.execute();//执行execute方法,返回结果
  33.                 assertEquals(Action.SUCCESS, result);//比对返回结果是否和UserAction中的执行结果一致
  34.         }


  35. }
复制代码


          二、编写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" })
  12. public class UserTestJunit {
  13.         @Autowired
  14.         UserService userService;// 自动注入userService

  15.         /**
  16.          * @description 测试查询用户
  17.          * @throws Exception
  18.          */
  19.         @Test
  20.         public void query() throws Exception {
  21.                 List result = userService.getAllEmployee();
  22.                 Assert.notEmpty(result);
  23.         }

  24.         /**
  25.          * @description 测试用户添加
  26.          * @throws Exception
  27.          */

  28.         @Test
  29.         public void save() throws Exception {
  30.                 User user = new User();
  31.                 user.setUsrCode("test001");
  32.                 user.setUsrName("test");
  33.                 user.setPassword("123");
  34.                 user.setIdCard("513029198503140026");
  35.                 user.setEmail("aaa@sina.com");
  36.                 User u = userService.save(user);
  37.                 Assert.notNull(u);
  38.                 org.junit.Assert.assertEquals("test", user.getUsrName());
  39.         }

  40.         /**
  41.          * @description 测试用户更新
  42.          * @throws Exception
  43.          */

  44.         @Test
  45.         public void update() throws Exception {
  46.                 User user = new User();
  47.                 user.setUsrCode("test001");
  48.                 user.setUsrName("test");
  49.                 user.setPassword("123");
  50.                 user.setIdCard("513029198503140026");
  51.                 user.setEmail("aaa@sina.com");
  52.                 User u = userService.update(user);
  53.                 Assert.notNull(u);
  54.                 org.junit.Assert.assertEquals("test", user.getUsrName());
  55.         }

  56.         /**
  57.          * @description 测试用户删除
  58.          * @throws Exception
  59.          */

  60.         @Test
  61.         public void del() throws Exception {
  62.                 User user = new User();
  63.                 user.setUserId("1");
  64.                 User u = userService.delete(user);
  65.                 Assert.notNull(u);
  66.                 org.junit.Assert.assertEquals("1", user.getUserId());
  67.         }
  68. }
复制代码
【总结】单元测试不仅限于此,灵活性比较大,要结合实际进行编写,上面两种测试是按照我们项目中规范编写,大家可以作为参考,自我觉得还是比较实用而且用注解方式比较方便。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值