我有一个基本的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>
你可以提供你的HTML代码,我倒了,而传递对象,你得到空值 –