spring集成Junit做單元測試及常見異常解決辦法

spring-test依賴包

 



  org.springframework
  spring-test
  4.3.7.RELEASE

如果想學習Java工程化、高性能及分佈式、深入淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群裡有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給大家。

1、簡單單元測試

package com.ssm.test; 
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ssm.dao.UserMngDao;
/**
* @author wangxiangyu
* @date:2017年7月18日 下午2:24:50
* 類說明:Spring單元測試
* 1、導入spring-test單元測試的jar包
* 2、@ContextConfiguration(locations={"classpath:applicationContext.xml"})指定Spring配置文件的位置
* 3、@RunWith(SpringJUnit4ClassRunner.class)使用Spring單元測試
* 3、直接autowired要使用的組件

*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class MapperTest {
@Autowired
UserMngDao userMngDao;

@Test
public void test1(){

List users = userMngDao.findAll();
for(Map user : users){
String staffName = null==user.get("staffName")?"":user.get("staffName").toString();
System.out.println(staffName);
}

}
}

2、模擬前端請求單元測試

package com.atguigu.crud.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.atguigu.crud.bean.Employee;
import com.github.pagehelper.PageInfo;
/**
* 使用Spring測試模塊提供的測試請求功能,測試curd請求的正確性
* Spring4測試的時候,需要servlet3.0的支持
* @author lfy
*

*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml" })
public class MvcTest {
// 傳入Springmvc的ioc
@Autowired
WebApplicationContext context;
// 虛擬mvc請求,獲取到處理結果。
MockMvc mockMvc;
@Before
public void initMokcMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testPage() throws Exception {
//模擬請求拿到返回值
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();

//請求成功以後,請求域中會有pageInfo;我們可以取出pageInfo進行驗證
MockHttpServletRequest request = result.getRequest();
PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
System.out.println("當前頁碼:"+pi.getPageNum());
System.out.println("總頁碼:"+pi.getPages());
System.out.println("總記錄數:"+pi.getTotal());
System.out.println("在頁面需要連續顯示的頁碼");
int[] nums = pi.getNavigatepageNums();
for (int i : nums) {
System.out.print(" "+i);
}

//獲取員工數據
List list = pi.getList();
for (Employee employee : list) {
System.out.println("ID:"+employee.getEmpId()+"==>Name:"+employee.getEmpName());
}

}
}

3、Junit常見異常及解決辦法

異常:

如果想學習Java工程化、高性能及分佈式、深入淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群裡有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給大家。

java.lang.IllegalStateException: Failed to load ApplicationContext

解決方法:

java1.8版本只支持spring4.0以上。所以解決方法有兩種:1)把sping版本換成4.0以上;2)把jdk調低點。此處我選擇把jdk調低點,再次運行。

異常:

java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException

解決方法:

沒有multipleFailureException類,可能是因為你的版本過低引起的,請更換最新版本。

異常:

java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test_sayHello4]

解決方法:

Spring-test版本與junit版本不兼容導致的,可以多試幾個版本。


分享到:


相關文章: