spring 如何使用 MockMvc 测试弹簧控制器方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14563489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test a spring controller method by using MockMvc?
提问by JToddler
I'm using spring 3.2.0 and junit 4
我正在使用 spring 3.2.0 和 junit 4
This is my controller method which I need to test
这是我需要测试的控制器方法
@RequestMapping(value="Home")
public ModelAndView returnHome(){
return new ModelAndView("Home");
}
spring-servlet config is:
spring-servlet 配置是:
<context:annotation-config/>
<context:component-scan base-package="com.spring.poc" />
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
This is my test class :
这是我的测试课:
public class TestController {
private MockMvc mockMvc;
@Before
public void setup() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
this.mockMvc = standaloneSetup(new CController()).setViewResolvers(
viewResolver).build();
}
@Test
public void CControllerTest() throws Exception {
......
......
}
}
}
How can I test this method with MockMvc ?
如何使用 MockMvc 测试此方法?
回答by Manuel Quinones
You can use your applications dispatcher servlet xml using the following annoations. The following example is hitting a controller with the path /mysessiontest setting some session attributes and expecting a certain view to be returned:
您可以使用以下注解来使用您的应用程序调度程序 servlet xml。以下示例使用路径 /mysessiontest 设置一些会话属性并期望返回某个视图:
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.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ "classpath:springDispatcher-servlet.xml" })
public class MySessionControllerTest {
@Autowired WebApplicationContext wac;
@Autowired MockHttpSession session;
@Autowired MockHttpServletRequest request;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void getAccount() throws Exception {
UserDomain user = new UserDomain();
user.setFirstName("johnny");
session.setAttribute("sessionParm",user);
this.mockMvc.perform(get("/mysessiontest").session(session)
.accept(MediaType.TEXT_HTML))
.andExpect(status().isOk())
.andExpect(view().name("test"));
}
}

