【问题标题】:How to test a spring controller method by using MockMvc?如何使用 MockMvc 测试弹簧控制器方法?
【发布时间】:2013-01-11 21:03:22
【问题描述】:

我正在使用 spring 3.2.0 和 junit 4

这是我需要测试的控制器方法

@RequestMapping(value="Home")
public ModelAndView returnHome(){

return new ModelAndView("Home");    
}

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" />

这是我的测试课:

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 {
    ......
    ......      
}

}

如何使用 MockMvc 测试此方法?

【问题讨论】:

    标签: spring junit integration-testing


    【解决方案1】:

    您可以使用以下注解来使用您的应用程序调度程序 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"));
        }
    }
    

    【讨论】:

    • @Mnauel Qunones:我尝试按照您的指示运行测试用例。但它未能运行。所有 springframe 工作 jar 都是相同的版本(3.2.0)。 .java.lang.Error:未解决的编译问题:get 无法解析为变量 Test1 类型的方法 status() 未定义 com.dummy.poc.Test1.homeTest 类型 Test1 的方法 view() 未定义(Test1.java:39) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    • 我会将所需的导入添加到我的示例中。
    • 编译器无法识别 get ,表示 get 无法解析为变量。在这一行: this.mockMvc.perform(get / "Home").accept(MediaType.TEXT_HTML) .andExpect(status().isOk()).andExpect(view().name("Home"));
    • 您是否添加了我添加到答案中的静态导入?这应该可以解决您的编译问题。
    • 这可以用junit测试相同的控制器方法吗?
    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2016-03-28
    相关资源
    最近更新 更多