dimanche 31 mars 2019

How to mock the baseUrl and sendredirect

I wrote this function, and I'm trying to write the jUnit Tests of this function. The first test runs, but the second one have some problems. Can you please help me?

This is the function

@GetMapping(value = "/productdetail/{productId}.pdf")
public void getProductDetailPdf(@PathVariable("productId") final String pProductId,
        final HttpServletResponse pResponse, final HttpServletRequest pRequest) throws Exception {
    final String pdfFileName = pProductId + ".pdf";
    pResponse.setContentType("application/pdf; charset=UTF-8");
    pResponse.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
    final String html = jsonProductDetailPdfFacade.showJsonProductDetailPdf(SessionUtil.get(pRequest), pProductId);
    if (!html.equals(EMPTY)) {
        pdfConverterFacade.htmlToPdf(html, pResponse);
    } else {
        final String url = pRequest.getRequestURL().toString();
        final String baseURL = url.substring(0, url.length() - pRequest.getRequestURI().length());
        final String redirect404 = baseURL + _404;
        pResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
        pResponse.sendRedirect(redirect404);
    }
}

This are the tests:

private static final String MIDDLE_URL = "middleUrl";
private static final String ANY_URL = "anyUrl";
private static final String _404 = "/404";
private static final String EMPTY_STRING = "";
private final static String ANY_PRODUCT_ID = "anyProductId";
private final static String ANY_HTML = "anyHtml";
private final static String EMPTY = EMPTY_STRING;
private final PdfConverterFacade pdfConverterFacade = mock(PdfConverterFacade.class);
private final JsonProductDetailPdfFacade jsonProductDetailPdfFacade = mock(JsonProductDetailPdfFacade.class);

private final PdfConverterController controller = new PdfConverterController(pdfConverterFacade,
        jsonProductDetailPdfFacade);

@Test
public void productDetailAsPdf() throws Exception {
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final SessionUtil sessionUtil = mock(SessionUtil.class);
    when(response.getContentType()).thenReturn("application/pdf; charset=UTF-8");
    when(SessionUtil.get(request)).thenReturn(sessionUtil);
    when(jsonProductDetailPdfFacade.showJsonProductDetailPdf(sessionUtil, ANY_PRODUCT_ID)).thenReturn(ANY_HTML);
    controller.getProductDetailPdf(ANY_PRODUCT_ID, response, request);
    verify(pdfConverterFacade).htmlToPdf(ANY_HTML, response);
}

@Test
public void productNotFound()throws Exception{
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final SessionUtil sessionUtil = mock(SessionUtil.class);
    when(request.getRequestURL().toString()).thenReturn(ANY_URL);
    when(ANY_URL.substring(0, ANY_URL.length() - request.getRequestURI().length())).thenReturn(MIDDLE_URL);
    when(SessionUtil.get(request)).thenReturn(sessionUtil);
    when(jsonProductDetailPdfFacade.showJsonProductDetailPdf(sessionUtil, ANY_PRODUCT_ID)).thenReturn(EMPTY_STRING);
    when(response.getStatus()).thenReturn(404);
    verify(response).sendRedirect(MIDDLE_URL + _404);
}

So the first tests works. the second one doesn't run. This is the else part of the function.

Can you please tell me what's wrong?

Aucun commentaire:

Enregistrer un commentaire