In AEM JAVA I created a function that gets a list of tags from Tag Manager . I was able to get the data that I need and the tags are correctly retrieved from the Tag Manager. But in the Unit Test this part below is always excluded from the test. I only included the code that is relevant to the question.
@SuppressWarnings("unchecked")
public Iterator<Resource> getTransform(Iterator<Tag> childTagItr, Locale locale, SlingHttpServletRequest request) {
return new TransformIterator(childTagItr, new Transformer() {
public Object transform(Object o) {
Tag tag = (Tag) o;
String tagId = tag.getTagID();
ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
vm.put("value", tagId);
vm.put("text", tag.getTitlePath(locale));
return new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), "nt:unstructured", vm);
}
});
}
I tried with below test :
@Test
void testGetDataTags() throws ServletException, IOException {
// Arrange
Mockito.when(request.getAttribute(Mockito.anyString())).thenReturn(slingBindings);
Mockito.when(slingBindings.getSling()).thenReturn(sling);
Mockito.when(request.getResourceResolver()).thenReturn(resolver);
Mockito.when(request.getResourceResolver().adaptTo(TagManager.class)).thenReturn(tagManager);
//Mockito.when(config.get(PATH)).thenReturn(PATH);
//Mockito.when(expressionHelper.getString(PATH)).thenReturn(PATH_VALUE);
Mockito.when(tagManager.resolve(Mockito.anyString())).thenReturn(parentTag);
getTags();
// Act
final Locale locale = request.getLocale();
CareerPageCategoryLevelTwo careerPageCategoryLevelTwo = new CareerPageCategoryLevelTwo();
Transformer tagResource = new Transformer() {
public Object transform(Object o) {
Tag tag = (Tag) o;
String tagId = tag.getTagID();
ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
vm.put("value", tagId);
vm.put("text", tag.getTitlePath(locale));
return new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), "nt:unstructured", vm);
}
};
@SuppressWarnings("unchecked")
Iterator<Resource> iteratorResource= new TransformIterator(tagIterator, tagResource);
Mockito.when(careerPageCategoryLevelTwo.getTransform(tagIterator, locale, request)).thenReturn(iteratorResource);
// Assert
assertNotNull(iteratorResource);
}
is excluded. My question is how can I include this part in the Unit Test?
Update: careerPageCategoryLevelTwo is the implementation class
In your test method testGetDataTags
you are mocking the method getTransform()
. You are returning the iteratorResource instead of the "real" TransformIterator inside the class under test CareerPageCategoryLevelTwo
.
You should change your line from
Mockito.when(careerPageCategoryLevelTwo.getTransform(tagIterator, locale request)).thenReturn(iteratorResource):
to
Iterator iteratorFromCareer = careerPageCategoryLevelTwo.getTransform(tagIterator, locale, request)
and you should do something with the iteratorFromCareer
- this should call the transform
method.
But I recommend you to use less Mockito and more Sling Mocks or AEM Mocks