Junit for Multifield values (Resource) | Community
Skip to main content
Level 5
April 17, 2023
Solved

Junit for Multifield values (Resource)

  • April 17, 2023
  • 3 replies
  • 2363 views

Hi,

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

 

@ChildResource
private java.util.List<Resource> customData;

 

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

 

Thanks in advance.

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 VeenaVikraman

@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.

 

Thanks

Veena ✌

3 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
April 17, 2023
partyush
Community Advisor
Community Advisor
April 17, 2023

Hi @aravinds 

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

 

@RunWith(MockitoJUnitRunner.class) public class CustomModelTest { @Mock private Resource resource; @Mock private ResourceResolver resourceResolver; @InjectMocks private CustomModel customModel = new CustomModel(); @Test 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<>(); childResources.add(childResource1); childResources.add(childResource2); // Mock the behavior of the resource resolver to return the list of child resources when(resourceResolver.getChildren(eq(resource))).thenReturn(childResources.iterator()); // 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.

Thanks

Partyush

aravindSAuthor
Level 5
April 18, 2023

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.

Thanks.

VeenaVikraman
Community Advisor
VeenaVikramanCommunity AdvisorAccepted solution
Community Advisor
April 20, 2023

@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.

 

Thanks

Veena ✌