gpt4 book ai didi

java - 编写用于下载文件的单元测试

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:39 25 4
gpt4 key购买 nike

目前我写了一个小的 downloadService 让用户下载一个文件(目前只有 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();
}

}
}

单元测试 - 尝试使用 Mockito,但出现异常:

单元测试:

package com.pzm.service;

import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.http.HttpServletResponse;
import static org.mockito.Mockito.*;

/**
* Created by akfaz on 7/5/14.
*/
public class DownloadServiceTest extends TestCase{

HttpServletResponse mockResponse;
DownloadService downloadService;

@Before
public void setUp() throws Exception {
mockResponse = mock(HttpServletResponse.class);
downloadService = new DownloadService();
}

@Test
public void testDownload() throws Exception {
downloadService.download(mockResponse, "xls");

verify(mockResponse).getContentType();
}
}

和异常:

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

最佳答案

创建模拟对象时

mockResponse = mock(HttpServletResponse.class);

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

所以这段代码中getOutputStream()的返回值

ServletOutputStream outputStream = response.getOutputStream();

null

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

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

这叫做 stubbing .

关于java - 编写用于下载文件的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24593624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com