Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Unit testing Sling Model that has similar code as of a core component (ContentFragmentListImpl)

Avatar

Level 1

I want to unit test the following Sling model, which has similar code as of the core component ContentFragmentListImpl. I tried to understand the unit test for ContentFragmentListImpl, but failed to do so. Any help is much appreciated.

 

Here is the code for the Sling model:

 

@Model( adaptables = {SlingHttpServletRequest.class, Resource.class},
        adapter = {
                CameraCardsList.class,
                ComponentExporter.class
        },
        resourceType = {CameraCardsListImpl.RESOURCE_TYPE_V1, CameraCardsListImpl.RESOURCE_TYPE_V2}
)
@Exporter()
public class CameraCardsListImpl extends AbstractComponentImpl implements CameraCardsList {

public static final String RESOURCE_TYPE_V1 = "core/wcm/components/contentfragmentlist/v1/contentfragmentlist"

public static final String RESOURCE_TYPE_V2 = "core/wcm/components/contentfragmentlist/v2/contentfragmentlist"
@Self(injectionStrategy = InjectionStrategy.REQUIRED)
private SlingHttpServletRequest slingHttpServletRequest;

@SlingObject
private ResourceResolver resourceResolver;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String modelPath;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String[] elementNames;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String parentPath;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private int maxItems;

@Self 
private GetFragment getFragment;

private final List<DAMContentFragment> items = new ArrayList<>();

@PostConstruct
public void init() {
// some code
}

}

 

 

Here is the the core component ContentFragmentListImpl:

 

 

@Model(
        adaptables = SlingHttpServletRequest.class,
        adapters = {
                ContentFragmentList.class,
                ComponentExporter.class
        },
        resourceType = {ContentFragmentListImpl.RESOURCE_TYPE_V1,ContentFragmentListImpl.RESOURCE_TYPE_V2}
)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class ContentFragmentListImpl extends AbstractComponentImpl implements ContentFragmentList {

    private static final Logger LOG = LoggerFactory.getLogger(ContentFragmentListImpl.class);

    public static final String RESOURCE_TYPE_V1 = "core/wcm/components/contentfragmentlist/v1/contentfragmentlist";
    public static final String RESOURCE_TYPE_V2 = "core/wcm/components/contentfragmentlist/v2/contentfragmentlist";

    public static final String DEFAULT_DAM_PARENT_PATH = "/content/dam";

    public static final int DEFAULT_MAX_ITEMS = -1;

    @Self(injectionStrategy = InjectionStrategy.REQUIRED)
    private SlingHttpServletRequest slingHttpServletRequest;

    @OSGiService
    private ContentTypeConverter contentTypeConverter;

    @SlingObject
    private ResourceResolver resourceResolver;

    @ValueMapValue(name = ContentFragmentList.PN_MODEL_PATH, injectionStrategy = InjectionStrategy.OPTIONAL)
    
    private String modelPath;

    @ValueMapValue(name = ContentFragmentList.PN_ELEMENT_NAMES, injectionStrategy = InjectionStrategy.OPTIONAL)
    
    private String[] elementNames;

    @ValueMapValue(name = ContentFragmentList.PN_TAG_NAMES, injectionStrategy = InjectionStrategy.OPTIONAL)
    
    private String[] tagNames;

    @ValueMapValue(name = ContentFragmentList.PN_PARENT_PATH, injectionStrategy = InjectionStrategy.OPTIONAL)
    
    private String parentPath;

    @ValueMapValue(name = ContentFragmentList.PN_MAX_ITEMS, injectionStrategy = InjectionStrategy.OPTIONAL)
    (intValues = DEFAULT_MAX_ITEMS)
    private int maxItems;

    @ValueMapValue(name = ContentFragmentList.PN_ORDER_BY, injectionStrategy = InjectionStrategy.OPTIONAL)
    (values = JcrConstants.JCR_CREATED)
    private String orderBy;

    @ValueMapValue(name = ContentFragmentList.PN_SORT_ORDER, injectionStrategy = InjectionStrategy.OPTIONAL)
    (values = Predicate.SORT_ASCENDING)
    private String sortOrder;

    private final List<DAMContentFragment> items = new ArrayList<>();

    @PostConstruct
    private void initModel() {
      // some code
    }

    
    
    public Collection<DAMContentFragment> getListItems() {
        return Collections.unmodifiableCollection(items);
    }

    
    
    public String getExportedType() {
        return slingHttpServletRequest.getResource().getResourceType();
    }
}

 

 

How would the unit test for CameraCardListImpl be like?

3 Replies

Avatar

Community Advisor

Hi, 
This is a standard JUnit test, but you need to be particularly careful about which services need to be mocked. Please refer to the instructions provided in these links to understand how to proceed:

 https://www.youtube.com/watch?v=g5x6F8bUHj8&ab_channel=AEMGEEKS 

https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/ 

 

Hope this helps.



Esteban Bustamante

Avatar

Administrator

@assyko Did you find the suggestion from user helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Administrator

@assyko Did you find the suggestion from user helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni