您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
使用Spring配合Junit進行單元測試的總結(jié)
作者:網(wǎng)絡轉(zhuǎn)載 發(fā)布時間:[ 2015/1/29 14:39:26 ] 推薦標簽:junit 軟件測試 單元測試

  近公司的項目和自己的項目中都用到了spring集成junit進行單元測試,總結(jié)一下幾種基本的用法:
  1.直接對spring中注入的bean進行測試(以DAO為例):
  在測試類上添加@RunWith注解指定使用springJunit的測試運行器,@ContextConfiguration注解指定測試用的spring配置文件的位置
  之后我們可以注入我們需要測試的bean進行測試,Junit在運行測試之前會先解析spring的配置文件,初始化spring中配置的bean
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:spring-config-test.xml"})
public class TestProjectDao {
@Autowired
ProjectDao projectDao;
@Test
public void testCreateProjectCode(){
long applyTime = System.currentTimeMillis();
Timestamp ts = new Timestamp(applyTime);
Map codeMap = projectDao.generateCode("5", "8",ts,"院內(nèi)");
String projectCode = (String)codeMap.get("_project_code");
Timestamp apply_time = (Timestamp)codeMap.get("_apply_time");
System.out.print(projectCode);
System.out.print(apply_time.toString());
Assert.assertTrue(projectCode.length()==12);
}
  2.對springMVC進行測試:
  spring3.2之后出現(xiàn)了org.springframework.test.web.servlet.MockMvc 類,對springMVC單元測試進行支持
  樣例如下:
package com.jiaoyiping.baseproject;
import com.jiaoyiping.baseproject.privilege.controller.MeunController;
import com.jiaoyiping.baseproject.training.bean.Person;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.ModelAndView;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Created with IntelliJ IDEA.
* User: 焦一平
* Date: 14-9-25
* Time: 下午6:45
* To change this template use File | Settings | File Templates.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
//@ContextConfiguration(classes = {WebMvcConfig.class, MockDataConfig.class})
@ContextConfiguration(locations={"classpath:/spring/applicationContext.xml", "classpath*:mvc-dispatcher-servlet.xml"})
public class TestMockMvc {
@Autowired
private org.springframework.web.context.WebApplicationContext context;
MockMvc mockMvc;
@Before
public void before() {
//可以對所有的controller來進行測試
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
//僅僅對單個Controller來進行測試
// mockMvc = MockMvcBuilders.standaloneSetup(new MeunController()).build();
}
@Test
public void testGetMenu(){
try {
System.out.println("----------------------------");
ResultActions actions =
this.mockMvc.perform(get("/menu/manage.action"));
System.out.println(status());//
System.out.println(content().toString());
actions.andExpect(status().isOk());
//            actions.andExpect(content().contentType("text/html"));
System.out.println("----------------------------");
} catch (Exception e) {
e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd