Spring MVC中编写单元测试(WEB项目):

1. 首先开发一个基类,用于载入配置文件。以下所有的测试实现类都要继承这个类

  1. package com.yusj.basecase;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.test.context.ContextConfiguration;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. /**
  8. * 配置文件载入类
  9. * @ClassName: BaseSpringTestCase
  10. * @Description: 要想实现Spring自动注入,必须继承此类
  11. * @author yusj
  12. * @date 2014年6月9日 下午3:16:44
  13. *
  14. */
  15. @RunWith(SpringJUnit4ClassRunner.class)
  16. @ContextConfiguration({
  17. "file:src/main/webapp/WEB-INF/config/applicationContext.xml",
  18. "file:src/main/webapp/WEB-INF/config/captcha-context.xml",
  19. "file:src/main/webapp/WEB-INF/config/springmvc-servlet.xml"
  20. })
  21. // 添加注释@Transactional 回滚对数据库操作
  22. @Transactional
  23. public class BaseSpringTestCase {
  24. }

用户登录测试方法UserControllerTest如下:

  1. package com.yusj.web.controller;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.fail;
  4. import java.sql.SQLException;
  5. import org.junit.Before;
  6. import org.junit.Ignore;
  7. import org.junit.Test;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.mock.web.MockHttpServletRequest;
  10. import org.springframework.mock.web.MockHttpServletResponse;
  11. import com.sim.tracker.basecase.BaseSpringTestCase;
  12. /**
  13. *
  14. * @ClassName: UserControllerTest
  15. * @Description: 测试用户控制类
  16. * @author yusj
  17. * @date 2014年5月18日
  18. *
  19. */
  20. public class UserControllerTest extends BaseSpringTestCase {
  21. // 模拟request,response
  22. private MockHttpServletRequest request;
  23. private MockHttpServletResponse response;
  24. // 注入userController
  25. @Autowired
  26. private UserController userController ;
  27. // 执行测试方法之前初始化模拟request,response
  28. @Before
  29. public void setUp(){
  30. request = new MockHttpServletRequest();
  31. request.setCharacterEncoding("UTF-8");
  32. response = new MockHttpServletResponse();
  33. }
  34. /**
  35. *
  36. * @Title:testLogin
  37. * @Description: 测试用户登录
  38. * @author yusj
  39. * @date 2014年5月18日
  40. */
  41. @Test
  42. public void testLogin() {
  43. String username= "aaaa" ;
  44. String password = "bbbb" ;
  45. try {
  46. assertEquals("loginok",userController.login(username, password, request)) ;
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }

注意:如果是Maven项目,当执行Maven install时,可能会报错误,造成不能正确生成war包。此时需要在pom.xml中加入如下配置:

  1. <project>
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-surefire-plugin</artifactId>
  7. <configuration>
  8. <testFailureIgnore>true</testFailureIgnore>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </build>
  13. </project>

注意:因为模拟request,response需要javax.servlet,AsycnContext类的支持,所以还需要导入javax.servlet3.0 Jar包的支持。

maven pom.xml配置代码如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>javax.servlet</groupId>
  4. <artifactId>javax.servlet-api</artifactId>
  5. <version>3.1.0</version>
  6. </dependency>
  7. </dependencies>

可以到http://www.mvnrepository.com/中输入关键字javax.servlet搜索下载。下载方式见我的另一篇文章:http://ysj5125094.iteye.com/blog/2082097

Spring MVC实现Junit Case的更多相关文章

  1. spring mvc 和junit 4集成的注意点

    常规步骤: 1.导入jar包,主要有两个,spring-test 和 junit4,主要用maven管理,直接依赖即可.可以在这个网站上进行查找或下载:http://mvnrepository.com ...

  2. Spring mvc中junit测试遇到com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException错误怎么解决

    今天遇到图片中的错误,纠结了一下,弄清楚了怎么从控制台中读取错误信息,并修改错误. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ...

  3. Spring MVC 项目搭建 -2- 添加service ,dao,junit

    Spring MVC 项目搭建 -2- 添加service ,dao,junit 1.dao public class Hero { private String name; public Strin ...

  4. JUnit+Mockito结合测试Spring MVC Controller

    [本文出自天外归云的博客园] 概要简述 利用JUnit结合Mockito,再加上spingframework自带的一些方法,就可以组合起来对Spring MVC中的Controller层进行测试. 在 ...

  5. spring Mvc + Mybatis 中使用junit

    在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/ja ...

  6. 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

    一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...

  7. Spring MVC 学习总结(一)——MVC概要与环境配置

    一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...

  8. [转]基于Spring + Spring MVC + Mybatis 高性能web构建

    http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...

  9. 基于Spring + Spring MVC + Mybatis 高性能web构建

    基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...

随机推荐

  1. sql语句变量定义和样例

    变量和与常量 1.定义和使用局部变量说明:局部变量是用户可自定义的变量,它的作用范围仅在程序内部.局部变量的名称是用户自定义的,命名的局部变量名要符合SQL Server 2000标识符命名规则,必须 ...

  2. iOS的UIDevice,NSBundle,NSLocale

    iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用 户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取 ...

  3. cnblogs开篇留念

    之前看过很多大牛程序员们介绍的一些经验之类的文章,几乎每个人都提到了一点就是平时要写博客,记录一些自己平时学习和工作过程中学习到的一些技术点和心得.之前也用过一些其他的网站博客,上周有同事推荐了一篇文 ...

  4. 事件委托能够优化js性能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 转发 通过NAT和防火墙特性和TCP穿透的测评(翻译)

    转自 http://blog.csdn.net/sjin_1314/article/details/18178329 原文:Characterization and Measurement of TC ...

  6. tabelView右滑选择进行删除

    如何使用UITableViewRowAction实现右滑选择呢? 1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只 ...

  7. 转载《Android Handler、Message》

    之前也是由于周末通宵看TI3比赛,一直没找到时间写博客,导致已经有好久没更新了.惭愧!后面还会恢复进度,尽量保证每周都写吧.这里也是先恭喜一下来自瑞典的Alliance战队夺得了TI3的冠军,希望明年 ...

  8. ajax 页面加载

    大体说说思路,不上代码了: 1.点击加载更多-> ajax向后台传参(当前页page,必须有默认1,其他需要的参数...) 2.后台接收 -> 查询数据 处理形成 json数据 给前台 3 ...

  9. 使用gnucash查看任意时间段内的所有者权益变动表

    gnucash默认会以当年的会计账期显示所有者权益变动表 如果要看指定时间的所有者权益变动表,需要这样做 打开所有者权益变动表 点击上面的[选项] 如图 在常规中自行选择日期 确定后就能够看到指定时间 ...

  10. 今天网站后台登录页面需要生成一个二维码,然后在手机app上扫描这个二维码,实现网站登录的效果及其解决方案如下

    要实现二维码登录,需要解决2个技术,1.需要js websocket 与后台php实现长连接技术 2.实现二维码生成技术 要实现这个功能第二个算是比较简单,只需要下载一个php的二维码生成器即可,但要 ...