Hello,
I have a test workflow written in aem where i need to get the Asset
where the java test case is written as:
JsonFile
{
"test":
{
"jcr:primaryType":"dam:Asset",
"jcr:createdBy":"admin"
}
}
please help me solve from above code on how to get the asset path/object so that i can retrive asset.getPath(), asset.getName() asset.getAssetMetadata() if required
for now its giving the value as null
request to share the sample code
Thanks & Regards.
Solved! Go to Solution.
Views
Replies
Total Likes
Correct. It will return null if you use the com.adobe.granite.asset.api.Asset
because it is not adaptable. Why are you using that? You should use the Asset implementation from com.day.cq.dam.api.Asset
.
If you really want to stick with the other Asset implementation, you need to mock that class and not rely on the aemContext
object. That's what I suggested as option #1 in my previous response. You should mock all of the following objects: ResourceResolver
, the AdaptTo
method, the AssetManager
, and the Asset
. The drawback with this approach is that you are not testing anything because you are setting the expected value at each step.
** Updated Response 10/07/25 **
As pointed out by @JackKi2, the implementation should not be changed to simplify writing test cases. You should prefer using com.adobe.granite.asset.api.Asset
whenever possible, as it is an upper abstraction in the general AEM stack
Hope this helps
Hi @VJain03 ,
Create an asset object using the below snippet in your Junit for the import com.adobe.granite.asset.api.Asset.
// to create an asset AssetManager assetManager = resolver.adaptTo(AssetManager.class); Asset newAsset = assetManager.createAsset("/path/to/asset/document.pdf"); // to get an existing asset Asset asset = assetManager.getAsset("/path/to/asset/document.pdf"); // to get asset by adapting Asset asset = resource.adaptTo(Asset.class);
Asset properties can be retrieved or set by adapting it to a ValueMap.
eg: // to get Asset properties value map ValueMap map = asset.adaptTo(ValueMap.class);
I hope this helps!!
Can you please share your code snippet here.
JsonFile
{
"test.mp4":
{
"jcr:primaryType":"dam:Asset",
"jcr:createdBy":"admin"
}
}
Please refer this thread for the same
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-create-junit-test-c...
The adaptTo is null, because the resource is not of type dam:Asset.
Similar discussion at: https://stackoverflow.com/questions/44807635/aem-junit-test-with-a-dam-asset
Thanks for the reply @aanchal-sikka
Here the resoure is of type dam:Asset
and i have checked
DamUtil.isAsset(resource);
and its returning true
but for
Asset asset = resource.adaptTo(Asset.class);
System.out.println(asset.getPath()); // Output: asset path: null
if the Asset is imported from "import com.day.cq.dam.api.Asset;" then its adapting properly
but when Asset is imported from "import com.adobe.granite.asset.api.Asset;" then its giving null
request you to please solve this
There are 2 ways to "mock" this type of "Asset.class", what I've read so far in your replies is that you are mixing both. Here are the options:
1) You use a mocked resourceResolver and then you must mock each method that will be invoked by this object (especially the adapTo) and manually return another mock object, and the cycle repeats.
2) You use aemContext.resourceResolver() and configure it to be adapted to a AssetManager.class,
My recommendation would be to go with method #2.
Usually, you would do something like this:
@Mock
private AssetManager mockAssetManager;
private AemContext context = new AemContext();
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
//This will register the adapTo(AssetManager)
context.registerAdapter(ResourceResolver.class, AssetManager.class, mockAssetManager);
}
@Test
void test_example() {
Asset asset = context.resourceResolver().getResource("/path/to/your/asset").adaptTo(Asset.class);
when(AssetManager.getAsset("/path/to/your/asset")).thenReturn(asset);
}
Check these references:
https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/
Hope this helps
Thank you for your reply @EstebanBustamante
Below code is giving null
please check it
Correct. It will return null if you use the com.adobe.granite.asset.api.Asset
because it is not adaptable. Why are you using that? You should use the Asset implementation from com.day.cq.dam.api.Asset
.
If you really want to stick with the other Asset implementation, you need to mock that class and not rely on the aemContext
object. That's what I suggested as option #1 in my previous response. You should mock all of the following objects: ResourceResolver
, the AdaptTo
method, the AssetManager
, and the Asset
. The drawback with this approach is that you are not testing anything because you are setting the expected value at each step.
** Updated Response 10/07/25 **
As pointed out by @JackKi2, the implementation should not be changed to simplify writing test cases. You should prefer using com.adobe.granite.asset.api.Asset
whenever possible, as it is an upper abstraction in the general AEM stack
Hope this helps
Thanks For your Reply @EstebanBustamante
In our project we are using "com.adobe.granite.asset.api.Asset" and we were told to use this api only as this is the latest one.
Hi Esteban,
Adobe's own documentation states to prefer the granite API over day.cq (please see here: Java™ API Best Practices in AEM | Adobe Experience Manager). It seems odd to suggest an older API for the solution when the only issue is with unittests. Is there a good reason to do so?
Thanks
Views
Replies
Total Likes
Hi @JackKi2,
Thanks for the clarification — I completely agree with you. You shouldn’t have to change your implementation just to accommodate your test cases. As you pointed out, it’s generally recommended to use the com.adobe.granite.asset implementation whenever possible. That said, I want to clarify that the com.day.cq implementation is not an “older API.” It simply interacts with different parts of the AEM stack, which can sometimes introduce issues depending on the type of operations or usage. While the Granite implementation is generally safer, the com.day version is not deprecated. A good example of this is the AEM Context framework, which currently does not yet support the Granite Asset implementation.
I'll update my response to reflect that. Thanks again for pointing this out!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies