2017-05-03 128 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

閱讀錯誤:但是:

相關問題