【发布时间】:2017-10-01 21:54:26
【问题描述】:
我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
我有这个控制器:
@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 代码吗,我认为在传递对象时你得到空值
标签: spring-mvc spring-boot thymeleaf junit4 mockmvc