1.需要配置,在测试用例跑起来之前,先启动spring和springmvc对应的容器(不然没法真正测试,)
具体的可行性代码如下
package com.hgh.laweye.controller;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
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.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.WebApplicationContext;
import com.itrus.laweye.controller.user.UserController;
/**
* 集成web环境测试
* @author heguanhong
*
*/
//@RunWith(JUnit4ClassRunner.class)
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/applicationContext*.xml")
public class UserControllerTest2 extends AbstractJUnit4SpringContextTests{
@Autowired
public WebApplicationContext webApplicationContext;
public MockHttpSession session;
private static final Logger log = LoggerFactory.getLogger(UserControllerTest2.class);
private MockMvc mockMvc;
@Before
public void Init(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testTest() {
MvcResult mvcResult = null;
try {
mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/user/test"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andDo(MockMvcResultHandlers.print())
.andReturn();
log.info("请求返回的状态码:" + mvcResult.getResponse().getStatus());
String result = mvcResult.getResponse().getContentAsString();
} catch (Exception e) {
e.printStackTrace();
}
fail("Not yet implemented");
}
}
仅仅测试controller,即:独立测试的情况
package com.hgh.laweye.controller;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.itrus.laweye.controller.user.UserController;
/**
* 独立测试的方式
* @author heguanhong
*
*/
public class UserControllerTest {
private static final Logger log = LoggerFactory.getLogger(UserControllerTest.class);
private MockMvc mockMvc;
@Before
public void Init(){
UserController userController = new UserController();
mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
}
@Test
public void testTest() {
MvcResult mvcResult = null;
try {
mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/user/test"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andDo(MockMvcResultHandlers.print())
.andReturn();
log.info("请求返回的状态码:" + mvcResult.getResponse().getStatus());
String result = mvcResult.getResponse().getContentAsString();
} catch (Exception e) {
e.printStackTrace();
}
fail("Not yet implemented");
}
}