junit Spring控制器模型和视图测试MockMvc空响应

wz1wpwve  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(148)

我有一个简单的示例设置来使用MockMvc对Spring Controller ModelAndView进行单元测试,但是响应总是空的,即使在调试中我可以看到控制器代码被执行。
下面是代码:

/源代码/主/java/控制器/MvcController.java

package controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MvcController {

    @RequestMapping("/hello/{id}")
    public ModelAndView hello(@PathVariable("id") String id) {
        Map<String, String> model = new HashMap<>();
        ModelAndView modelAndView = new ModelAndView("hello", model);
        return modelAndView;
    }

}

/源代码/主文件/Web应用程序/Web-INF/jsp/您好.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="e"
    uri="https://www.owasp.org/index.php/OWASP_Java_Encoder_Project"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>HELLO WORLD
    </body>
</html>

/源代码/测试/java/控制器/MvcControllerTest.java

package controller;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { MvcController.class })
public class MvcControllerTest {

    @Autowired
    private MvcController mvcController;

    private MockMvc mockMvc;

    @Before
    public void init() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        this.mockMvc = MockMvcBuilders.standaloneSetup(mvcController).setViewResolvers(resolver).build();
    }

    @Test
    public void testHello() throws Exception {
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello/XWQHEDg9e72t");
        ResultActions result = mockMvc.perform(requestBuilder);
        MvcResult mvcResult = result.andReturn();
        MockHttpServletResponse mockHttpServletResponse = mvcResult.getResponse();
        String response = mockHttpServletResponse.getContentAsString();
        assertThat(response).contains("HELLO WORLD");
    }

}
gkl3eglg

gkl3eglg1#

MockMvc中的关键词是 mock。这个框架没有使用JSP引擎设置完整的Servlet容器。因此,它不呈现控制器返回的视图,并且MockHttpServletResponse不包含主体(对于这个用例)。
您可以使用MockHttpServletResponse#getForwardedUrl()来获取Servlet容器将请求转发到的JSP的url,该url是根据视图名称和InternalResourceViewResolver的前缀和后缀构造的。

相关问题