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

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

Java代码  
  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如下:

Java代码  
  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中加入如下配置:

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配置代码如下:

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

转载于:https://www.cnblogs.com/ceshi2016/p/6247625.html

Spring MVC实现Junit Case相关推荐

  1. Spring MVC控制器JUnit测试

    JUnit测试Spring MVC控制器并非易事 . 但是最近,一个新项目 (即将在Spring推出)提供了新工具来简化此工作. 这篇文章说明了如何通过JUnit测试来测试一个简单的控制器. 该代码是 ...

  2. junit 测试mvc_Spring MVC控制器JUnit测试

    junit 测试mvc JUnit测试Spring MVC控制器并非易事 . 但是最近,一个新项目 (即将在Spring推出)提供了新的工具来简化此工作. 这篇文章说明了如何通过JUnit测试来测试一 ...

  3. spring mvc+junit

    为什么80%的码农都做不了架构师?>>>    spring mvc的简单单元测试,说白了就是测试spring mvc的controller. 先参考两篇帖子: 1.http://s ...

  4. idea junit 测试看不到控制台报错信息_高手都这么给 Spring MVC 做单元测试!

    本章节主要讲解以下两部分内容: 1.Mock 测试简介 2.测试用例演示 一.Mock 测试简介 1.什么是 mock 测试 在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个「虚拟的对象」 ...

  5. Spring MVC参数化测试 - Junit Parameterized

    参考文章:Spring MVC全注解配置 - 无web.xml 单元测试的目的,简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期修改后(不论是增加新功能,修改bug), ...

  6. 14.6 Spring MVC 测试框架(翻译)

    14.6 Spring MVC 测试框架(每天翻译一点点) Spring MVC测试框架对 Spring MVC 代码提供一流的测试支持 ,它拥有一个 fluent API ,可以和JUnit, Te ...

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

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

  8. Java之Spring mvc详解(非原创)

    文章大纲 一.Spring mvc介绍 二.Spring mvc代码实战 三.项目源码下载 四.参考文章 一.Spring mvc介绍 1. 什么是springmvc   springmvc是spri ...

  9. Spring MVC配置

    为什么80%的码农都做不了架构师?>>>    一.传统方式配置Spring MVC (1)导入jar包 需要导入如下的jar包 junit-3.8.1.jar         sp ...

最新文章

  1. 联发科mtk手机处理器怎么样_5G手机价格将再探新低!联发科天玑700芯片发布:入门级5G处理器...
  2. IntelliJ IDEA如何导入Gradle项目
  3. [html] 如何解决input在Firefox和Chrome中高度不一致的问题?
  4. C语言小游戏 ——俄罗斯方块
  5. 【例题+习题】【数值计算方法复习】【湘潭大学】(五)
  6. mysql与php6_PHP与MySQL的连接
  7. hdu 1195 Open the Lock
  8. MVC 《web考勤管理系统》 项目研发文献
  9. android斗鱼app源代码,android文件管理器源码、斗鱼直播源码、企业级erp源码等
  10. txt文件的编码结构
  11. python时间序列分析——基于混沌和数据分形理论的特征构建
  12. Windows编程语言VBA学习(四)——VBA代码调试
  13. 误差状态方程与雅可比矩阵
  14. win7不显示移动硬盘_Mac 下移动硬盘异常退出修复
  15. MySQL性能优化的最佳20+条经验
  16. 【数据结构与算法】学习笔记-《算法笔记》-7
  17. 基于abaqus的各向异性材料的抗拔力学性能分析
  18. 4 个分析 GameFi 项目的工具
  19. cerr与cout的区别
  20. java hh24miss_Java编程时间格式与数据库中时间格式转化

热门文章

  1. [GCJ] Qualification Round 2017
  2. HDU-5718 Oracle
  3. 【转】POP3、SMTP和IMAP之间的区别和联系
  4. Java基础知识强化之IO流笔记44:IO流练习之 复制图片的 4 种方式案例
  5. 查看一个进程对应的端口号
  6. Base64编码简介及在java中的使用
  7. html期末主题作业,tm.html
  8. 目标检测(Object Detection)综述--R-CNN/Fast R-CNN/Faster R-CNN/YOLO/SSD
  9. (19)VHDL实现流水灯
  10. (35)FPGA面试题FPGA工程师努力的方向