Unit Testing ChildResource
In Unit Test, @ChildResource annotation property always return null. Can anyone please guide me to resolve the issue?
Environment Details: AEM 6.5.16 and Java 11
CountryListImpl.java
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = { CountryList.class },
resourceType = CountryListImpl.RESOURCE_TYPE,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class CountryListImpl implements CountryList {
protected static final String RESOURCE_TYPE = "aem-demo/components/country-list";
protected static final String COUNTRY_LANGUAGE_LIST = "countryItems";
@ValueMapValue
@Named("title")
String defaultLabel;
@ChildResource(name = COUNTRY_LANGUAGE_LIST)
List< CountryItem > countryList;
public String getDefaultLabel() {
return defaultLabel;
}
public List<CountryItem> getCountryList() {
return countryList;
}
}
CountryItemImpl.java
@Model(
adaptables = Resource.class,
adapters = { CountryItem.class },
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class CountryItemImpl implements CountryItem {
@ValueMapValue
String country;
@ValueMapValue
String language;
@ValueMapValue
String link;
public String getCountry() {
return country;
}
public String getLanguage() {
return language;
}
public String getLink() {
return link;
}
}
CountryListImplTest.java
@ExtendWith(AemContextExtension.class)
public class CountryListImplTest {
private final AemContext ctx = new AemContext(ResourceResolverType.JCR_MOCK);
private CountryList countryList;
@BeforeEach
public void setup() {
ctx.load().json(
"/com/aem/demo/core/components/internal/models/v1/country-list.json",
"/content"
);
ctx.currentResource("/content/country-list");
countryList = Objects.requireNonNull(ctx.getService(ModelFactory.class))
.createModel(ctx.request(), CountryListImpl.class);
}
public void getCountryListTest() {
Assertions.assertNotNull(countryList);
// countryList.getCountryList() --> ALWAYS RETURN NULL
// Assertions.assertNotNull(countryList.getCountryList());
// Assertions.assertEquals(
// 2, countryList.getCountryList().size()
// );
}
}
country-list.json
{
"country-list": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "awcm-demo/components/country-list",
"title": "Select ...",
"countryItems": {
"jcr:primaryType": "nt:unstructured",
"item0": {
"jcr:primaryType": "nt:unstructured",
"country": "Austria",
"link": "/content/aem-demo/at/de",
"language": "German"
},
"item1": {
"jcr:primaryType": "nt:unstructured",
"country": "Canada",
"link": "/content/aem-demo/ca/en",
"language": "English"
}
}
}
}
