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

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

How to write Junit test cases for path based sling servlet for pdf generation using output service


Level 2

Can anyone please suggest to write Junit test cases for the following path based sling servlet?

-  Servlet for PDF generation, using output service with post method

- Takes two input parameter

  1. xdp from path /content/dam/formsanddocuments .
  2. xml data from postman.

- Using generatePDFOutput method of output service to generate pdf

Code snippet:

@component(service = { Servlet.class }, property = { SLING_SERVLET_METHODS + "=POST",

SLING_SERVLET_EXTENSIONS + "=json", SLING_SERVLET_PATHS + "=/bin/generatePDFService" })


public class PDFCreationServlet extends SlingAllMethodsServlet {


private static final String XDP_PATH = "/content/dam/formsanddocuments/";


private static String XML_PATH = "";


private static final long serialVersionUID = 1L;


private static final String SERVICE_USER_ACCOUNT = "serviceuser";





private transient ResourceResolverFactory resourceResolverFactory;



OutputService outputService;


public void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)

throws ServletException, IOException {


final Map<String, Object> serviceUserSession = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE,



try {

ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(serviceUserSession);

Resource res = resolver.getResource(XDP_PATH + "Autumn.xdp");


// getting xdp from repository and converting into document

Asset asset = res.adaptTo(Asset.class);

Resource xdp = asset.getOriginal();

InputStream xdpIS = xdp.adaptTo(InputStream.class);

Document xdpDocument = new Document(xdpIS);



// getting XML data via postman and converting into document

XML_PATH = request.getParameter("formData");

String xmlData=null;

String xmlFile = new String(Files.readAllBytes(Paths.get(XML_PATH)));

// xml parsing

xmlData = com.UticaPoC.parsing.XmlParserData.parseXmlData(xmlFile);

InputStream xmlIS = IOUtils.toInputStream(xmlData, StandardCharsets.UTF_8);

Document xmlDocument = new Document(xmlIS);


//output options

PDFOutputOptions outputOptions = new PDFOutputOptions();


Document doc;

doc = outputService.generatePDFOutput(xdpDocument, xmlDocument, outputOptions);


// Save the result in a PDF file

File pdfFile = new File("C:\\PDF's\\GeneratedPDF.pdf");




response.addHeader("Content-Disposition", "attachment; filename=output.pdf");

OutputStream responseOutputStream = response.getOutputStream();

IOUtils.copy(doc.getInputStream(), responseOutputStream);




} catch (OutputServiceException e) {


response.getWriter().write("OutputServiceException occurred: PDF generation failed. " + e.getMessage());

} catch (LoginException e) {

// TODO Auto-generated catch block


response.getWriter().write("LoginException occurred: " + e.getMessage());

} catch (Exception e) {

// TODO Auto-generated catch block


response.getWriter().write("Exception occurred: " + e.getMessage());




Any reference or suggestion will be helpful to write junit test cases for the above servlet.








1 Reply