Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a unit test for downloading a file

At the moment I wrote a small downloadService which let's user download a file (at the moment only excel). The code works properly, however I do not know how to write the unit test for it. That's my code:

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();
        }

    }
}

the unit test - tried to use Mockito, but got the exception:

unit test:

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();
    }
}

and the exception:

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
like image 682
pezetem Avatar asked Jul 06 '14 07:07

pezetem


People also ask

How do you write a unit test?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

What is the example of unit test?

A Real-world ExampleThe above unit test “asserts” that 5 + 10 is equal to 15. If the Add function returns anything else Assert. IsEqual result in error and the test case will fail. After you write your test cases, you will run them to verify that everything is working correctly.

What is unit test file?

Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure.

What is unit test and steps?

A unit test typically comprises of three stages: plan, cases and scripting and the unit test itself. In the first step, the unit test is prepared and reviewed. The next step is for the test cases and scripts to be made, then the code is tested.


1 Answers

When you create a mock object

mockResponse = mock(HttpServletResponse.class);

by default, all its methods that have a reference type return type (minus a few special cases) return null.

So the return value of getOutputStream() in this snippet

ServletOutputStream outputStream = response.getOutputStream();

is null.

You need to set expectations and specify a return value.

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

This is called stubbing.

like image 115
Sotirios Delimanolis Avatar answered Oct 11 '22 11:10

Sotirios Delimanolis