Expand my Community achievements bar.

AEM Assets JUnit Java Sample

Avatar

Level 1

Hi ! I'm trying to create a test case JUnit which involves getting the title metadata from image. But so far I can't find any materials or samples of Assets DAM JUnit test cases. Any suggestions will be helpful. Thank you 

3 Replies

Avatar

Community Advisor

Could you give the sample code you are trying to test ? Basically the logic of unit testing is mocking everything you want to test. If you have the correct mocks created , your test should basically pass. 

Avatar

Level 1

@VeenaVikraman please see below

 

public String getImageTitle() {
Resource resource = resolver.getResource(imagePath);
Asset asset = resource.adaptTo(Asset.class);
return asset.getMetadataValue("dc:Title");

 

Avatar

Level 1

You can create a new asset at "/content/dam/test-image.jpg" with width, height, mime type "image/jpeg" and image title

Example: 

Asset asset = context.create().asset("/content/dam/test-image.jpg", 1600, 900, "image/jpeg", "dc:title", "title metadata for image");

Do not need to load a binary and additional json.