首页 > 编程语言 > 详细

[转][Java]使用Spring配合Junit进行单元测试的总结

时间:2015-11-18 00:41:55      阅读:352      评论:0      收藏:0      [点我收藏+]

http://www.51testing.com/html/14/n-1408814.html

1.直接对spring中注入的bean进行测试(以DAO为例):

  在测试类上添加@RunWith注解指定使用springJunit的测试运行器,@ContextConfiguration注解指定测试用的spring配置文件的位置
  之后我们就可以注入我们需要测试的bean进行测试,Junit在运行测试之前会先解析spring的配置文件,初始化spring中配置的bean
技术分享
 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 
 3 @ContextConfiguration(locations={"classpath*:spring-config-test.xml"})
 4 
 5 public class TestProjectDao {
 6 
 7 @Autowired
 8 
 9 ProjectDao projectDao;
10 
11 @Test
12 
13 public void testCreateProjectCode(){
14 
15 long applyTime = System.currentTimeMillis();
16 
17 Timestamp ts = new Timestamp(applyTime);
18 
19 Map codeMap = projectDao.generateCode("5", "8",ts,"院内");
20 
21 String projectCode = (String)codeMap.get("_project_code");
22 
23 Timestamp apply_time = (Timestamp)codeMap.get("_apply_time");
24 
25 System.out.print(projectCode);
26 
27 System.out.print(apply_time.toString());
28 
29 Assert.assertTrue(projectCode.length()==12);
30 
31 }
View Code

 

 
2.对springMVC进行测试:
  spring3.2之后出现了org.springframework.test.web.servlet.MockMvc 类,对springMVC单元测试进行支持
  样例如下:
技术分享
  1 package com.jiaoyiping.baseproject;
  2 
  3 import com.jiaoyiping.baseproject.privilege.controller.MeunController;
  4 
  5 import com.jiaoyiping.baseproject.training.bean.Person;
  6 
  7 import junit.framework.Assert;
  8 
  9 import org.junit.Before;
 10 
 11 import org.junit.Test;
 12 
 13 import org.junit.runner.RunWith;
 14 
 15 import org.springframework.beans.factory.annotation.Autowired;
 16 
 17 import org.springframework.http.MediaType;
 18 
 19 import org.springframework.test.context.ContextConfiguration;
 20 
 21 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 22 
 23 import org.springframework.test.context.web.WebAppConfiguration;
 24 
 25 import org.springframework.test.web.servlet.MockMvc;
 26 
 27 import org.springframework.test.web.servlet.ResultActions;
 28 
 29 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 30 
 31 import org.springframework.web.servlet.ModelAndView;
 32 
 33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
 34 
 35 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
 36 
 37 /**
 38 
 39 * Created with IntelliJ IDEA.
 40 
 41 
 42 * Date: 14-9-25
 43 
 44 * Time: 下午6:45
 45 
 46 * To change this template use File | Settings | File Templates.
 47 
 48 */
 49 
 50 @RunWith(SpringJUnit4ClassRunner.class)
 51 
 52 @WebAppConfiguration
 53 
 54 //@ContextConfiguration(classes = {WebMvcConfig.class, MockDataConfig.class})
 55 
 56 @ContextConfiguration(locations={"classpath:/spring/applicationContext.xml", "classpath*:mvc-dispatcher-servlet.xml"})
 57 
 58 public class TestMockMvc {
 59 
 60 @Autowired
 61 
 62 private org.springframework.web.context.WebApplicationContext context;
 63 
 64 MockMvc mockMvc;
 65 
 66 @Before
 67 
 68 public void before() {
 69 
 70 //可以对所有的controller来进行测试
 71 
 72 mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
 73 
 74 //仅仅对单个Controller来进行测试
 75 
 76 // mockMvc = MockMvcBuilders.standaloneSetup(new MeunController()).build();
 77 
 78 }
 79 
 80 @Test
 81 
 82 public void testGetMenu(){
 83 
 84 try {
 85 
 86 System.out.println("----------------------------");
 87 
 88 ResultActions actions =
 89 
 90 this.mockMvc.perform(get("/menu/manage.action"));
 91 
 92 System.out.println(status());//
 93 
 94 System.out.println(content().toString());
 95 
 96 actions.andExpect(status().isOk());
 97 
 98 //            actions.andExpect(content().contentType("text/html"));
 99 
100 System.out.println("----------------------------");
101 
102 } catch (Exception e) {
103 
104 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
105 
106 }
107 
108 }
View Code

 

 

如果想使用TestNG,可以参考http://www.51testing.com/html/26/n-856526.html

如果想使用Spock,可以参考http://stackoverflow.com/questions/9811345/how-to-inject-spring-beans-into-spock-test

[转][Java]使用Spring配合Junit进行单元测试的总结

原文:http://www.cnblogs.com/buhaiqing/p/4973215.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!