【发布时间】:2017-02-20 20:18:15
【问题描述】:
我有一个应用程序,使用 java、thymeleaf 和 springboot。在主页 localhost:8080 用户必须输入一个值才能重定向到第二页“localhost:8080/getValues”
如何编写 junit 测试以便测试预期值?目前我的测试是因为找不到 404 页面,因为它取决于用户在主页上输入的值。
测试
@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
}
控制器
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(Locale locale) throws MalformedURLException {
ModelAndView model = new ModelAndView("index");
return model;
}
@RequestMapping(value = "/getValues", method = RequestMethod.POST)
public ModelAndView getValues(Info info) throws MalformedURLException {
ModelAndView model = new ModelAndView("getValues");
model.addObject("userID", info.userID());
Customer custinfo = index.readXML(info.userID());
model.addObject("custinfo", custinfo);
model.addObject("webHist", Web_HistoryRepo.getAll(info.userID()));
return model;
}
【问题讨论】:
-
你能展示一些控制器的外观代码吗?
-
@pezetem 我添加了我的控制器,你能告诉我如何将这个多页逻辑更改为 SPA 吗?我认为如果我将其更改为 SPA,它将删除依赖项
标签: java spring-boot junit