用thymeleaf测试spring-boot web-app

Jør*_*her 9 java spring spring-mvc spring-test thymeleaf

我正在尝试编写测试以确保我的控制器加载我的视图.

这样做时,我得到一个"圆形视图路径异常".这是由于百里叶视图解析器没有出现.

一个简单的控制器方法如下所示:

@Cacheable("Customers")
@RequestMapping(value="/customer",  method = RequestMethod.GET)
public String customer(Model model) {
    model.addAttribute("customer", "customer");
    return "customer";
}
Run Code Online (Sandbox Code Playgroud)

我的视图位于src/main/resources/templates(通过spring-boot进行autoconfig),在这个exapmle中,视图被命名customer.html.如果我将视图名称更改为与@requestMapping的值不同,那么我将避免错误ofc.

我如何提供Spring-boot-autoconfig为我的测试创建的ThymeleafViewResolver?

这个问题表明我必须这样做,但没有说明如何......:如何避免Spring MVC测试中的"循环视图路径"异常

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());  
    }
}
Run Code Online (Sandbox Code Playgroud)

例外

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)
Run Code Online (Sandbox Code Playgroud)

感谢所有回复提前ppl!

Jør*_*her 5

在这里发表了精彩的评论后,我将控制器更改为以下内容,现在可以正常运行:)

CustomerControllerTest.java

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class CustomerControllerTest {

    @Autowired
    CustomerController customerController;

    private MockMvc mockMvc;

    @Autowired
    WebApplicationContext wac;

    @Before
    public void setup(){

        // Process mock annotations
        MockitoAnnotations.initMocks(this);

        // Setup Spring test in webapp-mode (same config as spring-boot)
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testLoadCustomerPage() throws Exception{
        this.mockMvc.perform(get("/customer")).andExpect(status().isOk());  
    }
}
Run Code Online (Sandbox Code Playgroud)