Sling model unit test with constructor
Hi everyone
I am having some trouble testing a Sling model, My Sling interface looks like this:
@Model(adaptables= Resource.class)
public interface ImageFormItem {
/**
* @2007960 the title.
*/
@586265
String getTitle();
/**
* @2007960 the url.
*/
@586265
String getUrl();
/**
* @2007960 the image reference as a string.
*/
@586265
String getFileReference();
}
my sling model:
@Model(adaptables = Resource.class)
public class ImageFormItemImpl implements ImageFormItem {
private static final String PN_URL = "url";
private static final String PN_TITLE = "title";
private static final String PN_REFERENCE = "fileReference";
private SlingHttpServletRequest request;
private Resource options;
private ValueMap properties;
@586265
public ImageFormItemImpl(){ }
@586265
public ImageFormItemImpl(SlingHttpServletRequest request, Resource options, Resource option) {
this.request = request;
this.options = options;
this.properties = option.getValueMap();
}
@9944223
public String getTitle() {
return properties.get(PN_TITLE, String.class);
}
@9944223
public String getUrl() {
return properties.get(PN_URL, String.class);
}
@9944223
public String getFileReference() {
return properties.get(PN_REFERENCE, String.class);
}
}
and my test class:
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class ImageFormItemImplTest {
private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);
@Mock
ValueMap valueMap;
@Mock
private SlingHttpServletRequest request;
@Mock
private Resource options;
@Mock
private Resource properties;
@Mock
private ModelFactory modelFactory;
@InjectMocks
private ImageFormItemImpl imgFormItem = new ImageFormItemImpl();
@BeforeEach
void setUp() {
//registers the Sling Model to be tested, into the mock AEM Context, so it can be instantiated in the @2785667 methods.
aemContext.addModelsForClasses(ImageFormItemImpl.class);
//aemContext.registerService(ImageFormItemImpl.class,imgFormItem);
//aemContext.addModelsForClasses(ImageFormItemImpl.class);
//loads resource structures into the mock context, allowing the code to interact with these resources as if they were provided by a real repository.
aemContext.load().json("/com/biomerieux/adobe/core/models/impl/imageformitem.json", "/content/imageform");
}
@2785667
void getTitle() {
final String expected = "Facebook";
//ctx.currentResource sets the context of the mock resource to evaluate the code against,
aemContext.currentResource("/content/imageform/items/item1");
ImageFormItem imgFormItem = aemContext.request().adaptTo(ImageFormItem.class);;
String actual = imgFormItem.getTitle();
assertEquals(expected, actual);
}
@2785667
void getUrl() {
fail("Not yet implemented");
}
@2785667
void getFileReference() {
fail("Not yet implemented");
}
}
imageformitem.json:
{
"jcr:primaryType": "nt:unstructured",
"jcr:createdBy": "admin",
"jcr:lastModifiedBy": "admin",
"source": "local",
"jcr:created": "Fri Oct 29 2021 14:37:19 GMT+0200",
"jcr:lastModified": "Fri Oct 29 2021 14:39:08 GMT+0200",
"sling:resourceType": "onebmx/components/image-form",
"items": {
"jcr:primaryType": "nt:unstructured",
"item0": {"jcr:primaryType": "nt:unstructured"},
"item1": {
"jcr:primaryType": "nt:unstructured",
"fileReference": "/content/dam/onebmx/5771789.png",
"url": "https://www.youtube.com/",
"title": "youtube"
},
"item2": {
"jcr:primaryType": "nt:unstructured",
"fileReference": "/content/dam/onebmx/facebook.png",
"url": "https://fr-fr.facebook.com/biomerieux/",
"title": "facebook"
}
}
}
and compiling my code I get an error:
java.lang.NullPointerException
adobe.core.models.impl.ImageFormItemImplTest.getTitle(ImageFormItemImplTest.java:65)
Does anyone have any idea why i can't read the title in my json file, thanks in advance



