【问题标题】:testing spring-boot web-app with thymeleaf用 thymeleaf 测试 spring-boot web-app
【发布时间】: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


【解决方案1】:

在这里出色的 cmets 之后,我将控制器更改为以下,它现在可以工作了 :)

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());  
    }
}

【讨论】:

  • 请包含获取和状态的静态导入。 STS 不会自动解决它们。
  • SpringApplicationConfiguration 现已弃用,您可以改用 SpringBootTest
【解决方案2】:

我最终得到了这个解决方案。将 thymeleaf templates 目录从 src/main/resources 移动到 src/main/webapp/WEB-INF。同时调整ServletContextTemplateResolver中的prefix指向新位置/WEB-INF/templates/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 2016-11-05
    • 2018-11-29
    • 1970-01-01
    • 2017-11-02
    • 2017-05-26
    • 2019-06-28
    • 2019-07-22
    相关资源
    最近更新 更多