我有一个应用程序,使用java,thymeleaf和springboot。在主页localhost:8080用户必须输入一个值才能重定向到第二页“localhost:8080 / getValues”
如何编写junit测试以便测试预期值?目前我的测试结果为404页面未找到,因为它取决于用户在主页上输入的值。
测试
Collections.shuffle(nr);
nr.remove(0);
nr.remove(1);
nr.remove(2);
控制器
@Test
public void test() throws Exception {
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("index"))
.andDo(print());
//test passes
}
@Test
public void testVals() throws Exception {
this.mockMvc.perform(post("getValues"))
.andExpect(status().isNotFound()) //passes
.andExpect(model().attributeExists("webHist")); //fails //no modelandviewfound
}
答案 0 :(得分:0)
关于单元测试,您没有发送Info info
你错过了单位中的内容(),它应该是
this.mockMvc.perform(post("getValues"))
.contentType(MediaType.APPLICATION_JSON)
.content(INFO_IN_JSON_FORM)
.andExpect(status().isNotFound()) //passes
.andExpect(model().attributeExists("webHist"));
如果仍然无效,请在测试结束时添加.andDo(print())
,以便您获得有关结果的更多详细信息。
接下来我不知道整个Controller的样子,如果用@RestController注释,如果不是,你应该将@RequestBody添加到getValues()方法,以便从Json映射Info。
如果涉及到SPA,则不会返回ModelAndView,而是返回Json响应,并且您的前端会对其进行解释。