Expand my Community achievements bar.

SOLVED

How to Mock PDDocument.load in Junit5

Avatar

Employee

Hi Team,

 

I have a code like this in my service class.

try (ResourceResolver resourceResolver = request.getResourceResolver();PDDocument finalDoc = new PDDocument()){
requestStringJson = new JSONObject(IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8));
PDDocument pdfTemplate = null;
if(resourceResolver.getResource(/content/dam/XXXXXX/xxx/TEST_PDF.pdf) != null) {
Resource resource = resourceResolver.getResource(/content/dam/XXXXXX/xxx/TEST_PDF.pdf);
Asset pdfFile = resource.adaptTo(Asset.class);
pdfTemplate = PDDocument.load(pdfFile.getImagePreviewRendition().getStream());
//update fields
PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDTextField field;
field = (PDTextField) acroForm.getField(PDFConstants.NAME);

..................................................................

I am facing issue while writting junit for this class. 

@Test
void downloadPDFTest() throws ServiceException, IOException, JSONException {

String path = "/content/dam/XXXXXX/xxx/TEST_PDF.pdf";
try (MockedStatic<IOUtils> ioUtilsMockedStatic = Mockito.mockStatic(IOUtils.class)) {
ioUtilsMockedStatic.when(() -> IOUtils.toString((InputStream) any(), (Charset) any())).thenReturn("{\"name\":\"John Doe\"}");
when(request.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.getResource(path)).thenReturn(resource);
when(resource.adaptTo(Asset.class)).thenReturn(asset);
when(asset.getImagePreviewRendition()).thenReturn(rendition);
when(rendition.getStream()).thenReturn(inputStream);
when(PDDocument.load(inputStream)).thenReturn(document);



It is failing at this line throwing null pointer exception - when(PDDocument.load(inputStream)).thenReturn(document); . Can anyone share me the pointers or suggestions on is there a way to mock this in Junit.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi, 

 

The issue arises because you cannot mock the PDDocument object because it is a new object each time your code runs. This is caused by the following line:

try (ResourceResolver resourceResolver = request.getResourceResolver();PDDocument finalDoc = new PDDocument())

Therefore, even if you use when(PDDocument) to mock it, this will apply to a different object instance each time, preventing the expected return behavior.

 

You have two options to resolve this issue:

  • Refactor the code I pointed out above so that you can effectively mock the PDDocument object.

  • OR, ensure that the inputStream object passed as a parameter to PDDocument.load(inputStream) works correctly to generate the expected document. (This involves verifying functionality rather than mocking.)

I hope this helps!



Esteban Bustamante

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi, 

 

The issue arises because you cannot mock the PDDocument object because it is a new object each time your code runs. This is caused by the following line:

try (ResourceResolver resourceResolver = request.getResourceResolver();PDDocument finalDoc = new PDDocument())

Therefore, even if you use when(PDDocument) to mock it, this will apply to a different object instance each time, preventing the expected return behavior.

 

You have two options to resolve this issue:

  • Refactor the code I pointed out above so that you can effectively mock the PDDocument object.

  • OR, ensure that the inputStream object passed as a parameter to PDDocument.load(inputStream) works correctly to generate the expected document. (This involves verifying functionality rather than mocking.)

I hope this helps!



Esteban Bustamante

Avatar

Administrator

@EstebanBustamante Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni

Avatar

Community Advisor

@PraveenaTh please MARK the correct answer if this was useful to you.

 

cc: @kautuk_sahni 



Esteban Bustamante