[英]mockmvc test when the request Url and view name are the same
測試控制器:
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(method = GET)
public void test(Model model) {
}
}
聯合測試:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
public class Test2 {
@Test
public void testPage() throws Exception {
TestController controller = new TestController();
MockMvc mockMvc = standaloneSetup(controller).build();//.setSingleView(new InternalResourceView("/WEB-INF/views/test.jsp"))
mockMvc.perform(get("/test"))
.andExpect(view().name("test"));
}
}
運行JunitTest,出現異常:
javax.servlet.ServletException: Circular view path [test]: would dispatch back to the current handler URL [/test] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
我知道我可以使用 setSingleView,但我檢查了 StandaloneMockMvcBuilder 的 api
public StandaloneMockMvcBuilder setViewResolvers(ViewResolver... resolvers)
Set up view resolution with the given ViewResolvers. If not set, an InternalResourceViewResolver is used by default.
這意味着使用了 InternalResourceViewResolver,所以 servlet 將調度到“test.jsp”而不是“/test”,它不應該是一個圓圈,並且當我在瀏覽器中測試時它運行良好。或者 StandaloneMockMvcBuilder 不使用任何視圖解析器?
我使用的是 Spring MVC,而不是 Springboot,我遵循了這個並成功設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.