当前位置: 首页 > 知识库问答 >
问题:

使用thymeleaf测试spring boot web app

谭畅
2023-03-14

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

当这样做时,我得到一个“循环视图路径异常”。这是由于胸腺叶视图解析器不存在。

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

@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。如果我将视图名称更改为不同于@requestMapping的值,那么我可以避免C错误。

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

这个问题说我必须这么做,但没有说怎么做...:如何避免Spring MVC测试的“圆形视图路径”异常

@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)

谢谢大家提前回复ppl!

共有2个答案

邵昆琦
2023-03-14

我最终得到了这个解决方案。将thymeleaftemplate目录从src/main/Resources移动到src/main/webapp/WEB-INF。还要调整ServletContextTemplateResolver中的前缀以指向新位置/WEB-INF/template/

何兴邦
2023-03-14

在这里精彩的评论之后,我将控制器更改为以下内容,现在可以工作了:)

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());  
    }
}
 类似资料:
  • 我试图测试我的Spring MVC控制器,但我不断收到与Thymeleaf模板相关的错误。我真的不想在控制器测试中处理模板错误,因为这不是我真正感兴趣的。当模板不存在时让测试失败是可以的,但现在我收到了与根据错误代码找不到消息相关的错误。 当我运行应用程序时,这个问题不存在。我一直在尝试弄清楚如何设置测试环境来解决这个问题,但在那里我找不到任何有效的方法。现在,我只是真的想让控制器代码正常工作。

  • 我最近为Thymeleaf编写了一个自定义方言以及一个自定义处理器,以处理一些自定义标记,并在某些情况下用不同的标记替换它们,但我在编写处理器测试时遇到了问题: 类需要重写 方法,这是我需要测试的方法。 因为我的处理器涉及从获取变量,所以我尝试模拟它;但是,、和类都声明为final,这意味着它们不能被Mockito模拟。 我真的不想实例化一个实际的对象,因为它依赖于其他5个无法模拟的对象,我最终会

  • 我正在尝试测试一个Spring Boot 1.4.0.M3 MVC切片。控制器是这样的。 < code>productshow.html百里香模板的最小化视图如下。 测试类是这样的。 在运行测试时,我得到以下错误。 需要帮助来解决这个问题。提前感谢。

  • 问题内容: 我正在使用JavaScript测试运行程序“摩卡”。 我的测试失败了,因此我将使用进行调试。 但是运行测试时,没有输出(仅来自Mocha的测试结果)。看来Mocha已捕获并抑制了我的输出! 如何让Mocha显示输出?(对于失败的测试)? 编辑: 抱歉!- 在测试期间可以正常工作!我肯定一直期望它抑制输出,而且我没有正确检查自己的代码。感谢您的回应。所以…话虽如此…也许抑制通过测试的输出

  • 问题内容: 建议使用哪些方法来加快测试速度。 当连接断开或发生超时错误等时,我正在测试具有重试功能的网络库。但是,该库在重试之间使用a (因此在服务器重新启动时它不会连接数千次)。这个电话大大降低了单元测试的速度,我想知道有什么方法可以覆盖它。 请注意,我愿意实际更改代码,或使用模拟框架模拟Thread.sleep(),但想先听听您的意见/建议。 问题答案: 通常将与时间相关的功能委托给单独的组件

  • 问题 假如你正在使用 CoffeeScript 并且想要验证功能是否与预期一致,便可以决定使用 Nodeunit 测试框架。 讨论 Nodeunit 是一种 JavaScript 对于单元测试库( Unit Testing libraries )中 xUnit 族的实现,Java, Python, Ruby, Smalltalk 中均可以使用。 当使用 xUnit 族测试框架时,你需要将所需测试的