的TestController:
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(method = GET)
public void test(Model model) {
}
}
JunitTest:
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不使用任何查看解析器?