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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 编写下载文件的单元测试_Java_Unit Testing_Junit_Mockito - Fatal编程技术网

Java 编写下载文件的单元测试

Java 编写下载文件的单元测试,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,当时我写了一个小的下载服务,让用户下载一个文件(目前只有excel)。代码工作正常,但是我不知道如何为它编写单元测试。这是我的密码: package com.pzm.service; import com.pzm.model.UserBillingsMock; import com.pzm.model.report.ExcelReport; import com.pzm.model.report.Report; import com.pzm.model.report.ReportFactory

当时我写了一个小的下载服务,让用户下载一个文件(目前只有excel)。代码工作正常,但是我不知道如何为它编写单元测试。这是我的密码:

package com.pzm.service;

import com.pzm.model.UserBillingsMock;
import com.pzm.model.report.ExcelReport;
import com.pzm.model.report.Report;
import com.pzm.model.report.ReportFactory;
import org.springframework.stereotype.Repository;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by akfaz on 6/26/14.
 */

@Repository
public class DownloadService {

    private Report report;
    private List<UserBillings> userBillings;

    public void setBill(List<UserBillings> userBillings) {
        this.userBillings = userBillings;
    }

    public void download(HttpServletResponse response, String reportType) {

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

        report = new ReportFactory().create(reportType, userBillings);
        saveFile(response, report);
    }

    private void saveFile(HttpServletResponse response, Report report) {
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            report.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
例外情况是:

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : null
    at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:500)
    at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1417)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:179)
    at com.pzm.model.report.ExcelReport.write(ExcelReport.java:46)
    at com.pzm.service.DownloadService.saveFile(DownloadService.java:40)
    at com.pzm.service.DownloadService.download(DownloadService.java:34)
    at com.pzm.service.DownloadServiceTest.testDownload(DownloadServiceTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NullPointerException
    at java.util.zip.DeflaterOutputStream.<init>(DeflaterOutputStream.java:84)
    at java.util.zip.DeflaterOutputStream.<init>(DeflaterOutputStream.java:142)
    at java.util.zip.ZipOutputStream.<init>(ZipOutputStream.java:118)
    at java.util.zip.ZipOutputStream.<init>(ZipOutputStream.java:104)
    at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:433)
    ... 28 more
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:保存失败:保存包时出错:null
位于org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:500)
位于org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1417)
在org.apache.poi.POIXMLDocument.write上(POIXMLDocument.java:179)
在com.pzm.model.report.ExcelReport.write(ExcelReport.java:46)上
位于com.pzm.service.DownloadService.saveFile(DownloadService.java:40)
下载(DownloadService.java:34)
下载com.pzm.service.DownloadServiceTest.testDownload(DownloadServiceTest.java:32)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:157)
位于com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
位于com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
位于com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
原因:java.lang.NullPointerException
位于java.util.zip.DeflaterOutputStream。(DeflaterOutputStream.java:84)
位于java.util.zip.DeflaterOutputStream。(DeflaterOutputStream.java:142)
位于java.util.zip.ZipoutStream。(ZipOutputStream.java:118)
位于java.util.zip.ZipoutStream。(ZipOutputStream.java:104)
位于org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:433)
... 28多

创建模拟对象时

mockResponse = mock(HttpServletResponse.class);
默认情况下,其所有具有引用类型返回类型(减去一些特殊情况)的方法都返回
null

因此,此代码段中的
getOutputStream()
的返回值

ServletOutputStream outputStream = response.getOutputStream();
null

您需要设置期望值并指定返回值

when(mockResponse.getOutputStream().thenReturn(/* the value to return when that method is invoked */);

这被称为。

您正在模拟HttpServletResponse。因此,在此模拟响应上调用getOutputStream()将返回null,除非在调用此方法时告诉模拟返回OutputStream。您是否可以尝试
Mockito.spy()
而不是
Mockito.mock()
来创建
mockResponse
?+1这是正确的做法。顺便说一句,您的第一句话并不完全正确-有些引用类型的Mockito默认值不为null,例如collections@DavidWallace Any
Collection
subtype?我不知道。嗯,是的,你在回答中提到的“少数特殊情况”如下。(1) 默认情况下,返回任何包装类型的方法将返回0或false。(2) 默认情况下,返回
集合的任何子类型的方法将返回空集合。(3) 默认情况下,标准
toString
的任何实现都将返回模拟的名称。(4) 默认情况下,标准
compareTo
的任何实现都将返回1。
when(mockResponse.getOutputStream().thenReturn(/* the value to return when that method is invoked */);