Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

AEM Servlet Unit Test

ihsan_a
Level 1
Level 1

I have the servlet below to use com.adobe.fd.forms.api.FormsService. I'm trying to create a unit test (see below) but I'm getting the following error. It's failing at line 33 of the servlet:

Document xmlDocument = new Document(xmlData.getBytes());

It seems that the servelt is not initialized correctly or messing the references. Can you please help?

Error:

Servlet:

import java.io.IOException;

import javax.servlet.Servlet;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.io.IOUtils;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.HttpConstants;

import org.civ.forms.core.services.document.FormsDocumentServices;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.aemfd.docmanager.Document;

@Component(service = Servlet.class,

property =

{ Constants.SERVICE_DESCRIPTION + "= ",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.paths=/bin/render",

"sling.auth.requirements=/bin/render"

})

public class FormRenderServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

@Reference

private transient FormsDocumentServices documentServices;

private static final String APPLICATION_PDF = "application/pdf";

private final transient Logger logger = LoggerFactory.getLogger(getClass());

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

try {

String xdpName = request.getParameter("xdpname");

String xmlData = request.getParameter("xmldata");

Document xmlDocument = new Document(xmlData.getBytes());

Document renderedPDF = documentServices.render(xdpName, xmlDocument);

String pdfFileName = xdpName.replace(".xdp", ".pdf");

response.setContentType(APPLICATION_PDF);

response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);

response.setContentLength((int) renderedPDF.length());

IOUtils.copy(renderedPDF.getInputStream(), response.getOutputStream());

} catch (Exception e) {

response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);

logger.error("reading or writing opertion failed : {}", e);

}

}

}

Test:

import static org.junit.Assert.assertEquals;

import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.when;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.testing.mock.sling.junit.SlingContext;

import org.eclipse.jetty.http.HttpStatus;

import org.junit.Rule;

import org.junit.Test;

public class FormRenderServletTest {

    @Rule

    public final SlingContext context = new SlingContext();

    private FormRenderServlet fixture = new FormRenderServlet();

    @Test

    public void doGet() throws ServletException, IOException {

    SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);      

        SlingHttpServletResponse response = mock(SlingHttpServletResponse.class);   

       

        when(request.getParameter("xdpname")).thenReturn("xdpTemplate.xdp");

        when(request.getParameter("xmldata")).thenReturn("");

        fixture.doPost(request, response);

        assertEquals(HttpStatus.OK_200, response.getStatus());

    }

}

5 Replies
abhinashs680544
Level 1
Level 1

Hi ihsan, I am facing same issue. Have you find the solution. If yes, please share

narindera257447
Level 2
Level 2

Use @RunWith(MockitoJUnitRunner.class) on top of your classname since you are using Mockito.

narindera257447
Level 2
Level 2

Also for third party API , you might need to use Mockito spy just trying having a check on it as well.

pradeep_kumar_3
Level 2
Level 2

Hi Narindra, It will be great if you can share the sample code.

abhinash007
Level 2
Level 2

Hi Everyone,

     You need to add Adobe AEMDS Core Document Manager Support (adobe-aemds-core-docmanager) bundle in classpath or you can add it dependency. This bundle is present in AEM. It will resolve DocumentSourceHandler class

1818982_pastedImage_0.png