mardi 2 avril 2019

How to test a sendredirect in java

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 = "\"http://any.url\"";
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()).thenReturn(new StringBuffer(ANY_URL));
    when(request.getRequestURI()).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.

this is my error:

Wanted but not invoked: httpServletResponse.sendRedirect( "middleUrl/404" ); -> at com.bechtle.terra.warp.web.pdf.PdfConverterControllerTest.productNotFound(PdfConverterControllerTest.java:52) Actually, there were zero interactions with this mock.

at com.bechtle.terra.warp.web.pdf.PdfConverterControllerTest.productNotFound(PdfConverterControllerTest.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

Aucun commentaire:

Enregistrer un commentaire