问题描述
我正在学习Java Spring,并尝试编写一个控制器方法的测试。也就是说,我想通过其路径执行一个控制器方法,并检查返回的内容是否包含一些符号序列。例如,我的测试是通过地址“ user / 1”调用页面上的内容,并期望该内容包含“ root”。
不幸的是,尽管可以在浏览器中输入此路由并获得该用户的页面而没有任何问题,但是执行“ user / 1”将返回空内容。
这是我要测试的控制器方法
@GetMapping("/{id}")
public String view(@PathVariable(value = "id") Integer id,Model model) throws EntityNotFoundException {
try {
User item = users.get(id);
model.addAttribute("item",item);
model.addAttribute("posts",posts.getLatestByUser(item,10));
model.addAttribute("comments",comments.getLatestByUser(item,10));
return "user/view";
} catch (IllegalArgumentException ex) {
throw new EntityNotFoundException("Cannot find a user with id = " + id);
}
}
这是我要测试的课程
@SpringBootTest(classes = {
Main.class,TestDataConfig.class,TestWebConfig.class
})
@ActiveProfiles("test")
@AutoConfigureMockMvc
@Sql(value = {"/create-tables.sql","/fill-tables.sql"},executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public class UserCtrlTest {
@Autowired
private UserCtrl controller;
@Autowired
private MockMvc mockMvc;
@Test
@WithAnonymousUser
public void shouldDisplayUserInfoUponWatchingUserPage() throws Exception {
MvcResult result = this.mockMvc.perform(get("/user/1"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(view().name("user/view"))
.andReturn();
String stringResult = result.getResponse().getContentAsString();
Assert.assertTrue(stringResult.contains("<h4>root</h4>"));
}
}
这是我的视图解析器类
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
这是方法.andDo(print())在控制台中返回的结果
MockHttpServletRequest:
HTTP Method = GET
Request URI = /user/1
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = ru.job4j.forum.controller.UserCtrl
Method = ru.job4j.forum.controller.UserCtrl#view(Integer,Model)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = user/view
View = null
Attribute = item
value = ru.job4j.forum.model.User@20
errors = []
Attribute = posts
value = [ru.job4j.forum.model.Post@20,ru.job4j.forum.model.Post@21]
Attribute = comments
value = [ru.job4j.forum.model.Comment@21,ru.job4j.forum.model.Comment@24]
Attribute = user
value = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Language:"en",X-Content-Type-Options:"nosniff",X-XSS-Protection:"1; mode=block",Cache-Control:"no-cache,no-store,max-age=0,must-revalidate",Pragma:"no-cache",Expires:"0",X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = /WEB-INF/views/user/view.jsp
Redirected URL = null
Cookies = []
MockHttpServletRequest:
HTTP Method = GET
Request URI = /user/1
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = ru.job4j.forum.controller.UserCtrl
Method = ru.job4j.forum.controller.UserCtrl#view(Integer,X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = /WEB-INF/views/user/view.jsp
Redirected URL = null
Cookies = []
您能帮我理解为什么请求没有返回页面内容吗?以及如何解决这个问题?如果您需要其他资源和屏幕截图来澄清这种情况,请写下来,我将添加它们。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)