1、使用Spring-Test进行普通测试
【1】导入Junit和Spring-Test的架包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- Junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
【2】创建Junit测试类
【3】在测试类上添加注解
@RunWith()
、@ContextConfiguration()
即可
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-conf/spring-service.xml"})
public class ServiceTest {
@Resource
private TblDeptService service;
@Test
public void test_insert() {
System.out.println(service == null);
}
}
2、使用Spring-Test进行Web请求测试
Spring MVC 测试框架是基于Servlet API mock对象(在spring 的org.springframework.mock.web包中提供),因此无需使用运行时servlet容器。
条件1:Spring4测试的时候,需要servlet3.0的支持。
条件2:因为需要测试请求,所以需要把spring web的配置文件加入。
【1】测试类配置
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.ContextConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
// 如果在webapp下,则:"file:src/webapp/WEB-INF/spring-web.xml"
@ContextConfiguration(locations = {"classpath:spring-config/spring-service.xml", "classpath:spring-config/spring-web.xml"})
public class MyMvcControllerTest {
....
}
@RunWith是JUnit的注解,指定SpringJUnit4ClassRunner
覆盖确实的org.junit.runners.BlockJUnit4ClassRunner
,其也是BlockJUnit4ClassRunner
的子类。
@WebAppConfiguration用于指定为测试加载WebApplicationContext。
@ContextConfiguration用于指定怎么加载 Spring 容器中的元数据。
【2】设置@Before对象
在Before注解的方法中,我们需要初始化spring特定的mock对象:MockMvc,spring mvc服务器端测试支持的主入口。
public class EmpControllerTest {
@Resource
private WebApplicationContext wac;
private MockMvc mockmvc;
@Before
public void testBefore() {
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(wac);
mockMvc = builder.build();
}
MockMvcBuilders 是访问特定MockMvcBuilder的实现接口的主要类。
【3】编写Test方法
@Test
public void testGoList() throws Exception {
/*
mockmvc.perform()模拟请求
MockMvcRequestBuilders - 获取模拟的Request对象
get()、delete()、post()、put() 分别代表模拟的请求
param()代表请求的参数 key/value;
andReturn()表示返回的结果
*/
MvcResult result = mockmvc.perform(MockMvcRequestBuilders.get("/list").param("pageNum", "10")).andReturn();
// 使用MvcResult对象获取HttpRequest对象
MockHttpServletRequest request = result.getRequest();
// 获取参数信息
PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
}
【4】完整Web请求测试类
@RunWith(SpringJUnit4ClassRunner.class)
// 如果在webapp下,则:"file:src/webapp/WEB-INF/spring-web.xml"
@ContextConfiguration(locations = {"classpath:spring-config/spring-service.xml", "classpath:spring-config/spring-web.xml"})
@WebAppConfiguration
public class EmpControllerTest {
@Resource
private WebApplicationContext context;
private MockMvc mockmvc;
@Before
public void testBefore() {
mockmvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testGoList() throws Exception {
MvcResult result = mockmvc.perform(MockMvcRequestBuilders.get("/list").param("pageNum", "10")).andReturn();
MockHttpServletRequest request = result.getRequest();
PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
}
}