簡(jiǎn)述
在spingMVC框架中,所有的類都是通過(guò)springMVC容器來(lái)加載和實(shí)例化的。
因此在junit測(cè)試的時(shí)候可以在其初始化方法中去加載spring配置文件。所有用Junit進(jìn)行單元測(cè)試,都需要下面的配置。注:配置文件的加載根據(jù)自己項(xiàng)目的實(shí)際情況進(jìn)行配置。
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:config/spring-mvc.xml",
"classpath:config/spring-resources.xml",
"classpath:config/spring-application.xml",
"classpath:config/spring-security.xml" })
Dao層的單元測(cè)試
通過(guò)依賴注入的方式注入測(cè)試對(duì)象;
package com.lczyfz.zerobdt.test;
import com.lczyfz.zerobdt.modules.test.dao.TestDao;
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 org.springframework.transaction.annotation.Transactional;
import static junit.framework.TestCase.assertEquals;
/**
* Created by maple on 2018-07-01.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration(locations = {"/spring-context.xml"})
public class TestDaoTests {
@Autowired
TestDao testDao;
@Test
public void testFindAllList(){
System.out.println(testDao.findAllList(new com.lczyfz.zerobdt.modules.test.entity.Test()).toString());
assertEquals(testDao.findAllList(new com.lczyfz.zerobdt.modules.test.entity.Test()).size(),13);
}
}
成功:

失?。?/p>

Service層的單元測(cè)試
和Dao層類似;
package com.lczyfz.zerobdt.test;
import com.lczyfz.zerobdt.modules.test.service.TestService;
import junit.framework.TestCase;
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 static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.*;
/**
* Created by maple on 2018-07-01.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring-context.xml"})
public class TestDaoServices {
@Autowired
TestService testService;
@Test
public void testFindAllList(){
System.out.println(testService.get("5").toString());
assertNotNull(testService.get("5"));
}
}
Controller層的單元測(cè)試
package com.lczyfz.zerobdt.test;
import com.lczyfz.zerobdt.common.test.SpringTransactionalContextTests;
import com.lczyfz.zerobdt.modules.test.web.TestController;
import org.junit.Before;
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 org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.assertNotNull;
/**
* Created by maple on 2018-07-01.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring-context.xml","/spring-mvc.xml"})
public class TestDaoControllers extends SpringTransactionalContextTests{
@Autowired
TestController testController;
MockMvc mockMvc;
@Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
}
@Test
public void testFindAllList(){
ResultActions resultActions = null;
try {
resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/show_user").param("id", "1"));
} catch (Exception e) {
e.printStackTrace();
}
MvcResult mvcResult = resultActions.andReturn();
String result = null;
try {
result = mvcResult.getResponse().getContentAsString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("=====客戶端獲得反饋數(shù)據(jù):" + result);
// 也可以從response里面取狀態(tài)碼,header,cookies...
// System.out.println(mvcResult.getResponse().getStatus());
}
}
Controller層的測(cè)試建議采用Postman的方式來(lái)測(cè)試,可以記錄url,同時(shí)也更符合實(shí)際情況。