I am trying to write tests to make sure my controllers load my views.
In doing so, I get a "circular view of the path" exception. This is due to the fact that thimelear-species-resolver is not present.
A simple controller method is as follows:
@Cacheable("Customers") @RequestMapping(value="/customer", method = RequestMethod.GET) public String customer(Model model) { model.addAttribute("customer", "customer"); return "customer"; }
My views are located on src/main/resources/templates (autoconfig by spring-boot), in which case the view is called customer.html . If I change the view name that is different from the @requestMapping value, then I avoid the c error.
How can I provide the ThymeleafViewResolver that spring-boot-autoconfig creates for my tests?
This question says that I have to do this, but I don’t know how ..: How to avoid the "Circular viewing path". Spring MVC test exception
CustomerControllerTest.java
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class CustomerControllerTest { @Autowired CustomerController customerController; private MockMvc mockMvc; @Before public void setup(){
An exception
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)
Thanks for all the answers in advance ppl!
java spring spring-mvc spring-test thymeleaf
Jørgen Skår Fischer
source share