JUNIT for DAM asset | Community
Skip to main content
TarunKumar
Community Advisor
Community Advisor
June 7, 2023
Solved

JUNIT for DAM asset

  • June 7, 2023
  • 3 replies
  • 2252 views

I am trying to write JUNIT using AEMMockitoExtension. 
But below line of code is giving null in main class. I have loaded my json file where ":jcr:data" is also provided for binary type asset which is text.

asset.getOriginal().getStream()

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by TarunKumar

I was able to fix this, loading asset directly from JSON file seems to create issue for "getStream" function, as method doesn't recognize "jcr:data" value of JSON as binary stream.
To overcome this I created a asset and added a rendition to it with below line of code

Asset asset = context.create().asset("/content/dam/sample/sample.txt", 1, 1, "text/plain"); asset.addRendition("original", new ByteArrayInputStream("Convert to bytes".getBytes(StandardCharsets.UTF_8)), "jcr:data");





3 replies

Vaibhavi_J
Level 7
June 7, 2023

Hi @tarunkumar ,

 

Firstly, please check asset.getOriginal() returns value. If the value is null, the operation on null throws Nullpointer.


Anyways you can use the below code to Mock.

 

Rendition rendition = mock(Rendition.class);
Asset mockAsset = mock(Asset.class);
when(rendition.getStream()).thenReturn(this.getClass().getClassLoader().getResourceAsStream("image/<image-name>.jpg"));

 

TarunKumar
Community Advisor
Community Advisor
June 8, 2023

Hi @vaibhavi_j ,

asset.getOriginal() returns value but the method getStream() returns null. My asset is already mocked as I am directly loading from json file, so I believe no need to mock explicitly as suggested above. So if I follow the above suggested approach then asset that I am loading through JSON file will not take place.
The issue is that "jcr:data" property is not being read from JSON file.

Thanks
Tarun

Sady_Rifat
Community Advisor
Community Advisor
June 7, 2023

I tried previously but failed to load asset directly from Json.
You can do one thing,

Asset asset = context.create().asset("/content/dam/mydoc.pdf", 1, 1, "application/pdf"); // Then use 'when' to initialize the asset based on you implementation // when(initializerFunction()).thenReturn(asset);
TarunKumar
Community Advisor
TarunKumarCommunity AdvisorAuthorAccepted solution
Community Advisor
June 9, 2023

I was able to fix this, loading asset directly from JSON file seems to create issue for "getStream" function, as method doesn't recognize "jcr:data" value of JSON as binary stream.
To overcome this I created a asset and added a rendition to it with below line of code

Asset asset = context.create().asset("/content/dam/sample/sample.txt", 1, 1, "text/plain"); asset.addRendition("original", new ByteArrayInputStream("Convert to bytes".getBytes(StandardCharsets.UTF_8)), "jcr:data");





Sady_Rifat
Community Advisor
Community Advisor
June 9, 2023

Yes, the same thing I told you to do create a mock asset and send it to the original class.