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.
Solved! Go to Solution.
Views
Replies
Total Likes
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!
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!
@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!
Views
Replies
Total Likes
@PraveenaTh please MARK the correct answer if this was useful to you.
cc: @kautuk_sahni