经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » JUnit » 查看文章
ssh(spring,struts2,hibernate)框架整合junit4
来源:cnblogs  作者:冰湖一角  时间:2018/9/25 19:08:59  对本文有异议

step1:导入必须的包,如果是maven项目,直接在pom.xml文件里加入以下依赖包:

  1.     <dependency>
  2.             <groupId>junit</groupId>
  3.             <artifactId>junit</artifactId>
  4.             <version>4.12</version>
  5.             <scope>test</scope>
  6.         </dependency>
  7.         <dependency>
  8.             <groupId>org.springframework</groupId>  
  9.             <artifactId>spring-test</artifactId>  
  10.             <version>2.5.5</version>  
  11.             <scope>test</scope>  
  12.         </dependency> 
  13.         <!-- 测试action-->
  14.         <dependency>
  15.             <groupId>org.apache.struts</groupId>
  16.             <artifactId>struts2-junit-plugin</artifactId>
  17.             <version>2.3.15.1</version>
  18.             <scope>test</scope>
  19.         </dependency>
  20.         <!-- 模拟http请求 -->
  21.         <dependency>
  22.             <groupId>javax.servlet</groupId>
  23.             <artifactId>jsp-api</artifactId>
  24.             <version>2.0</version>
  25.             <scope>test</scope>
  26.         </dependency>

step2:在src/main/java下建测试类

step2.1:测试action

  1. //测试action的类需要继承StrutsSpringTestCasepublic class JunitTrackActionTest extends StrutsSpringTestCase{
  2.  
  3.     @Testpublic void test() {//设置 请求参数request.setParameter("startTime", "2018-09-11 00:00:00");
  4.         request.setParameter("endTime", "2018-09-11 12:00:00");
  5.         request.setParameter("queryReliability", "99");try {//括号里的是前台发送的url连接String result = executeAction("/trackAction!getTrackByParams.action");
  6.             System.out.println(result);
  7.         } catch (UnsupportedEncodingException e) {
  8.             e.printStackTrace();
  9.         } catch (ServletException e) {
  10.             e.printStackTrace();
  11.         }
  12.     }
  13. }

step2.2:测试service

  1. public class JunitTrackServiceTest {private String startTime;private String endTime;private String queryReliability;private TrackService trackService;
  2.  
  3.     @Beforepublic void init() {
  4.         ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
  5.         trackService = (TrackService) classPathXmlApplicationContext.getBean("trackService");
  6.  
  7.         startTime = "2018-09-11 00:00:00";
  8.         endTime = "2018-09-11 12:00:00";
  9.         queryReliability = "99";
  10.     }
  11.     
  12.     @Testpublic void test() {
  13.         List<Track> list = trackService.getTrackByParams(startTime, endTime, Integer.parseInt(queryReliability));
  14.         
  15.         System.out.println(list.get(0).getCaptureTime());
  16.     }
  17.  
  18. }

step2.3:测试dao

  1. public class JunitTrackDaoTest {private String startTime;private String endTime;private String queryReliability;private TrackDAO trackDAO;
  2.  
  3.     @Beforepublic void init() {
  4.         ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
  5.         trackDAO = (TrackDAO) classPathXmlApplicationContext.getBean("trackDAO");
  6.  
  7.         startTime = "2018-09-11 00:00:00";
  8.         endTime = "2018-09-11 12:00:00";
  9.         queryReliability = "99";
  10.     }
  11.     
  12.     @Testpublic void test() {
  13.         List<Track> list = trackDAO.getTrackByParams(startTime, endTime, Integer.parseInt(queryReliability));        ///JSONObject json = JSONObject.fromObject(list.get(0));System.out.println(list.get(0).getCaptureTime());
  14.  
  15.     }
  16.  
  17. }

最后,点击鼠标右键--》run as--->JUnit Test,即可

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号