【发布时间】:2014-02-18 00:55:25
【问题描述】:
我正在尝试编写测试以确保我的控制器加载我的视图。
执行此操作时,我得到一个“圆形视图路径异常”。这是因为 thymeleaf-view-resolver 不存在。
一个简单的控制器方法如下所示:
@Cacheable("Customers")
@RequestMapping(value="/customer", method = RequestMethod.GET)
public String customer(Model model) {
model.addAttribute("customer", "customer");
return "customer";
}
我的视图位于src/main/resources/templates(spring-boot 自动配置),在这个例子中,视图被命名为customer.html。如果我将 view-name 更改为与 @requestMapping 的值不同,则可以避免 c 的错误。
如何将 Spring-boot-autoconfig 创建的 ThymeleafViewResolver 提供给我的测试?
这个问题表明我必须这样做,但没有说如何..:How to avoid the "Circular view path" exception with Spring MVC test
CustomerControllerTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class CustomerControllerTest {
@Autowired
CustomerController customerController;
private MockMvc mockMvc;
@Before
public void setup(){
// Process mock annotations
MockitoAnnotations.initMocks(this);
// Setup Spring test in stand-alone mode
this.mockMvc = MockMvcBuilders.standaloneSetup(customerController).build();
}
@Test
public void testLoadCustomerPage() throws Exception{
this.mockMvc.perform(get("/customer")).andExpect(status().isOk());
}
}
异常
javax.servlet.ServletException: Circular view path [customer]: would dispatch back to the current handler URL [/customer] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:263)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:186)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
at org.springframework.test.web.servlet.TestDispatcherServlet.render(TestDispatcherServlet.java:119)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:170)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
at com.***.***.salesweb.web.controller.CustomerControllerTest.testLoadCustomerPage(CustomerControllerTest.java:51)
感谢大家提前回复!
【问题讨论】:
-
您正在测试控制器而没有任何上下文存在(即您在独立模式下运行)。对于集成测试,请使用
webAppContextSetup而不是standaloneSetup。 -
感谢您的评论 -> 不知道这是可能的。发布了对我原始帖子的更新以给出答案。
-
@kingon 把你的答案放在答案部分:)
标签: java spring spring-mvc thymeleaf spring-test