Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 Rest微服务中的模拟HSSF工作簿_Java_Unit Testing_Nullpointerexception_Mockito_Hssfworkbook - Fatal编程技术网

Java Rest微服务中的模拟HSSF工作簿

Java Rest微服务中的模拟HSSF工作簿,java,unit-testing,nullpointerexception,mockito,hssfworkbook,Java,Unit Testing,Nullpointerexception,Mockito,Hssfworkbook,我正在处理一个案例,其中我需要为处理HSSF工作簿的控制器编写单元测试。 但是,即使在测试类中创建了对象并通过Mockitowhen/thenReturn传递了它,我仍然在wb.write(outByteStream)得到空指针异常。 以下是我的控制器方法: @PostMapping(value = "/recordsToExcel") public void exportModelsHistoryDataToExcel(HttpServletRequest request,

我正在处理一个案例,其中我需要为处理HSSF工作簿的控制器编写单元测试。 但是,即使在测试类中创建了对象并通过Mockito
when/thenReturn
传递了它,我仍然在
wb.write(outByteStream)得到空指针异常。
以下是我的控制器方法:

@PostMapping(value = "/recordsToExcel")
public void exportModelsHistoryDataToExcel(HttpServletRequest request,
        @RequestBody ModelsDto modelsDto) {
    int minimum = 10;

    int rand = new Random().nextInt(1000);

    int randomNum = minimum + rand;
    String excelFileName = "Revisions" + randomNum + ".xls";

    HSSFWorkbook wb = service.getModelSearchExcel(modelsDto);

    try {
        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        wb.write(outByteStream);
        byte[] outArray = outByteStream.toByteArray();
        response.setContentType("application/vnd.ms-excel");
        response.setContentLength(outArray.length);
        response.setHeader("Expires:", "0"); 
        response.setHeader("Content-Disposition", "attachment; filename=" + excelFileName);
        OutputStream outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
        outStream.close();

        wb.close();
    } catch (Exception e) {
        logger.error("ModelController - exportModelsHistoryDataToExcel() - exception::", e);
    }

}
下面是我的测试方法,我也尝试过在测试方法中创建一个工作表,但没有成功。 不确定我错过了什么或做错了什么。请帮忙

@Test
public void testExportModelsHistoryDataToExcel() throws Exception {
     ModelsDto modelsDto = new ModelsDto();
    modelsDto.setBirthFrom("9/9/1989");
//TODO Mock objects instead of creating
    HSSFWorkbook wb = new HSSFWorkbook(); //final class can't mock
    ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
    wb.write(outByteStream);
    when(service.getModelSearchExcel(modelsDto)).thenReturn(wb);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/recordsToExcel")
            .contentType(MediaType.APPLICATION_JSON).content(convertToJson(modelsDto));
    this.mockMvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk());
}