@Inject annotation in AEM JUnit
Hi all,
I have many Sling Models and I need to create a JUnit test for them, but I'am not able to deal with the @586265 annotation.
Here a simple example:
Sling Model:
import lombok.Getter;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@Model(adaptables = {SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SimpleModel {
protected final Logger logger = LoggerFactory.getLogger(getClass());
@Inject
protected Resource currentResource;
@Getter
private List<Resource> items;
@PostConstruct
protected void init() {
items = new ArrayList<>();
try {
Resource parsys = currentResource.getChild("parsys");
if (parsys != null) {
for(Resource child : parsys.getChildren()) {
items.add(child);
}
}
} catch (Exception ex) {
logger.error("Unexpected error: ", ex);
}
}
}
Test:
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextBuilder;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.impl.injectors.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class SimpleModelTest{
private AemContext context = new AemContext();
private SimpleModel simpleModel;
@BeforeEach
public void setup() throws Exception {
context.addModelsForClasses(SimpleModel.class);
context.load().json("SimpleModelTest.json", "/content/myproject/us");
currentResource = context.currentResource("/content/myproject/us/jcr:content/simple-component");
context.request().setResource(currentResource);
simpleModel = context.request().adaptTo(SimpleModel.class);
}
@Test
void testGetRowsItems() {
//Build expected result
Resource parsys = currentResource.getChild("parsys");
List<Resource> expectedResult = new ImmutableList.Builder<Resource>()
.add(parsys.getChild("item1"))
.add(parsys.getChild("item2"))
.build();
//Build actual result
List<Resource> actualResult = simpleModel.getItems();
//Check the actual result
assertEquals(expectedResult.size(), actualResult.size());
}
}
The actualResult.size() gives me 0 because the @586265 currentResource is not "executed" (currentResource is null).
If I change the annotation from @586265 to @SlingObject the test goes well but of course I want that my tests work with @586265 annotation.
Could anyone help me?
Thanks!