Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写依赖于索引页的junit测试?_Java_Spring Boot_Junit - Fatal编程技术网

Java 如何编写依赖于索引页的junit测试?

Java 如何编写依赖于索引页的junit测试?,java,spring-boot,junit,Java,Spring Boot,Junit,我有一个应用程序,使用java、thymeleaf和springboot。在主页localhost:8080上,用户必须输入一个值以重定向到第二页“localhost:8080/getValues” 如何编写junit测试以便测试预期值?目前,我的测试显示404页面未找到,因为它取决于用户在主页上输入的值 测试 @Test public void test() throws Exception { this.mockMvc.perform(get("/")) .andExpec

我有一个应用程序,使用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;
    }

关于单元测试,您没有发送
Info
您缺少单元中的content(),它应该是

 this.mockMvc.perform(post("getValues"))
        .contentType(MediaType.APPLICATION_JSON)
        .content(INFO_IN_JSON_FORM)
        .andExpect(status().isNotFound()) //passes
        .andExpect(model().attributeExists("webHist"));
如果仍然不起作用,请在测试结束时添加
.andDo(print())
,这样您将收到有关结果的更多详细信息

接下来,我不知道您的整个控制器是什么样子的,如果它是用@RestController注释的,那么您应该将@RequestBody添加到getValues()方法中,以便从Json映射信息


如果涉及到SPA,您不返回ModelAndView,而是返回Json响应,前端将对其进行解释。

关于单元测试,您没有发送
信息
您缺少单元中的content(),它应该是

 this.mockMvc.perform(post("getValues"))
        .contentType(MediaType.APPLICATION_JSON)
        .content(INFO_IN_JSON_FORM)
        .andExpect(status().isNotFound()) //passes
        .andExpect(model().attributeExists("webHist"));
如果仍然不起作用,请在测试结束时添加
.andDo(print())
,这样您将收到有关结果的更多详细信息

接下来,我不知道您的整个控制器是什么样子的,如果它是用@RestController注释的,那么您应该将@RequestBody添加到getValues()方法中,以便从Json映射信息


如果涉及到SPA,您不返回ModelAndView,而是返回Json响应,前端会对其进行解释。

您能展示一些控制器的代码吗?@pezetem我添加了我的控制器,您能告诉我如何将此多页逻辑更改为SPA吗?我想如果我将其更改为SPA,它将删除依赖关系。你能展示一些控制器的代码吗?@pezetem我添加了控制器,你能告诉我如何将此多页逻辑更改为SPA吗?我想如果我把它改成SPA,它会删除依赖关系。内容不起作用。我尝试导入MediaType。但也出现了错误。“APPLICATION_JSON无法解析或不是字段”import org.springframework.httpMy controller的注释为@controller。如果不使用ModelAndView,流程是什么?你能给我一个url,我可以把它转换成SPA。对于SPA,你可以尝试使用angular教程(),我相信你在你的代码中使用JSP,而SPA是不可能的。我正在使用Thymeleaf、Java、Spring boot和angularjs。没有JSP.ok,那么为什么要使用ModelAndView呢?您应该从angularJS调用一些端点,作为响应,从Spring Boot接收Json。内容不起作用。我尝试导入MediaType。但也出现了错误。“APPLICATION_JSON无法解析或不是字段”import org.springframework.httpMy controller的注释为@controller。如果不使用ModelAndView,流程是什么?你能给我一个url,我可以把它转换成SPA。对于SPA,你可以尝试使用angular教程(),我相信你在你的代码中使用JSP,而SPA是不可能的。我正在使用Thymeleaf、Java、Spring boot和angularjs。没有JSP.ok,那么为什么要使用ModelAndView呢?您应该从angularJS调用一些端点,作为响应,从Spring Boot接收Json