2017-05-03 129 views
0

我有一个基本的SpringBoot应用程序。使用Spring初始化程序,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行JAR文件。Spring Boot - 使用Thymeleaf的MockMVC forwardedUrl

我有这样的控制器:

@Controller 
@RequestMapping("/deviceevent") 
public class DeviceEventController { 

    @RequestMapping(value={ "/list"}, method = { RequestMethod.GET}) 
    public String deviceeventList() { 

     return "tdk/deviceEvent/DeviceEventList"; 
    } 
} 

和这个其他测试类。使用Spring的MockMVC框架进行测试。这在测试驱动的MVC应用程序,就好像是在一个容器中运行,

@RunWith(SpringJUnit4ClassRunner.class) 
    @WebAppConfiguration 
    @WebMvcTest 
    public class MockMvcTests { 

     // Pull in the application context created by @ContextConfiguration 
     @Autowired 
     private WebApplicationContext wac; 

     private MockMvc mockMvc; 

     @Before 
     public void setup() { 
      // Setup MockMVC to use our Spring Configuration 
      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 
     } 


     @Test 
     public void getDeviceEventsTest() throws Exception { 
      this.mockMvc 
        .perform(get("/deviceevent/list") // 
          .accept(MediaType.parseMediaType("text/html;charset=UTF-8"))) 
        .andExpect(status().isOk()) // 
        .andExpect(model().size(1)) // 
.andExpect(forwardedUrl("tdk/deviceEvent/DeviceEventList")); 
     } 

但我得到了在转发URL此错误。我总是在JSP中使用这种方法,从来没有在Thymeleaf,但我想这是一样的:

java.lang.AssertionError: Forwarded URL expected:</tdk/deviceEvent/DeviceEventList> but was:<null> 
+0

你可以提供你的HTML代码,我倒了,而传递对象,你得到空值 –

回答

0

假设一个标准的Thymeleaf/Spring设置,它看起来像是控制器是doi的一个误解ng - 当控制器返回字符串"tdk/deviceEvent/DeviceEventList"时,它不是在某处转发HTTP请求,而是返回一个视图名称。

在普通的Spring-thymeleaf设置中,该字符串对应于将在点击该端点时渲染的thymeleaf视图的名称(我假设控制器只是提供正常的网页 - 以便该路径可能对应于某个文件最有可能的路径是src/main/resources - 但是这又取决于你的spring配置) - 此时HTTP请求还没有返回给用户,Spring仍在处理它 - 并且将尝试在返回之前呈现HTML视图给用户。

如果Spring没有呈现任何内容,而是使用301/302机制向用户返回HTTP响应以将其转发给另一个URL(这将启动不同的Spring请求响应进程),则使用转发的URL。

注意在下面的方法的不同:

@RequestMapping(value="/document", method=RequestMethod.GET) 
public String newDocumentSettings(Model model){ 
    model.addAllAttributes(contentManagementService.documentToJson()); 
    return "pages/document-settings"; 
} 


@RequestMapping(value="/document", method=RequestMethod.POST) 
public String createNewDocument(@RequestParam String title, @RequestParam String overview, @RequestParam String tags){ 
    Document doc = documentService.createDocument(title, overview, tags); 
    return "redirect:/document/${doc.url}/1?getting-started"; 
} 

首先呈现在给定文件路径的模板,第二返回重定向命令给浏览器,使另一个HTTP请求到指定的网址。

在任何情况下,您测试用例中的forwardedUrl是因为hte HTTP Response没有要转发的URL(因为它返回了HTML)。如果你确实需要转发行为(例如你实际上想完成响应和浏览器发出第二个HTTP请求),那么你可能需要按照例子更新控制器,但是,如果你对呈现的html页面满意,那么测试是无效的(查看Thymeleaf测试框架以了解如何测试模板)。注意:这是基于默认的Spring-Boot配置的假设 - 如果您有其他配置,该字符串确实会导致转发的HTTP请求,那么这不适用!

0

隔空猜在这里,但URL tdk/deviceEvent/DeviceEventList可能是没有定义。尝试与您的上下文相关的网址替换它(根据需要编辑):

@Test 
    public void getDeviceEventsTest() throws Exception { 
     this.mockMvc 
       .perform(get("/deviceevent/list") 
         .accept(MediaType.parseMediaType("text/html;charset=UTF-8"))) 
       .andExpect(status().isOk()) 
       .andExpect(model().size(1)) 
       .andExpect(forwardedUrl("/WEB-INF/tdk/deviceEvent/DeviceEventList.html")); 
    } 

一旁,而不是:

@RequestMapping(value={ "/list"}, method = { RequestMethod.GET})

你可以使用速记:

@GetMapping("/list")

+0

阅读错误:但是:

相关问题