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()
Solved! Go to Solution.
Views
Replies
Total Likes
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");
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"));
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
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);
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");
Yes, the same thing I told you to do create a mock asset and send it to the original class.
Views
Likes
Replies