Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Junit for Multifield values (Resource)


Level 4


Here I'm using Resource to take the multifield in sling model, can anyone help how to write junit for this method. 


private java.util.List<Resource> customData;


public java.util.List<Resource> getCustomData() {
return customData;


Thanks in advance.

1 Accepted Solution


Correct answer by
Community Advisor
4 Replies


Community Advisor

Hi @aravindS 

Sure, here's an example JUnit test for your getCustomData method in your Sling model.


public class CustomModelTest {

  private Resource resource;

  private ResourceResolver resourceResolver;

  private CustomModel customModel = new CustomModel();

  public void testGetCustomData() {
    // Create a mock child resource with a "name" property
    Map<String, Object> properties = new HashMap<>();
    properties.put("name", "Test Name");
    Resource childResource1 = new ResourceMock("childResource1", properties);

    // Create another mock child resource with a different "name" property
    Map<String, Object> properties2 = new HashMap<>();
    properties2.put("name", "Another Name");
    Resource childResource2 = new ResourceMock("childResource2", properties2);

    // Create a list of child resources and add the two mock child resources
    List<Resource> childResources = new ArrayList<>();

    // Mock the behavior of the resource resolver to return the list of child resources

    // Set the resource resolver on the custom model
    Whitebox.setInternalState(customModel, "resourceResolver", resourceResolver);

    // Call the getCustomData method and verify that it returns the correct list of resources
    List<Resource> customData = customModel.getCustomData();
    assertEquals(2, customData.size());
    assertEquals("Test Name", customData.get(0).getValueMap().get("name"));
    assertEquals("Another Name", customData.get(1).getValueMap().get("name"));



This JUnit test uses the Mockito framework to create mock objects for the Resource and ResourceResolver interfaces, and then sets up the necessary behavior for the resourceResolver mock object to return a list of two mock child resources when its getChildren method is called.

The test then calls the getCustomData method on the customModel object and verifies that it returns a list of two resources, with the correct property values.

Note that in this example, the CustomModel class is assumed to be the Sling model class that contains the getCustomData method, and that it has been annotated with the @Model annotation to make it a Sling model.




Level 4

Hi Partyush,

Thanks for your reply.

I have added the code, but have 2 errors, what is eq in when method and what is the package for whitebox.



Correct answer by
Community Advisor

@aravindS I tried to write a sample here https://medium.com/@veena.vikraman19/aem-junit-for-simple-aem-component-with-multifield-2326f61c8982 . Please check if this helps.



Veena ✌