Views
Replies
Total Likes
Hi,
You can inject resourecResolver and adapt to Node
resourceRsolver.adaptTo(Node.class)
How can I inject resourceResolver in test class?
Hi,
You can get it from context
ResourceResolver rr = context.resourceResolver()
Do you use the Sling Testing Framework?
a
We are injecting Node:
@inject
@source("script-bindings")
Node currentNode;
This is my method in sling model where we are using currentNode.
public void setSeparator() {
if (tileClass.equalsIgnoreCase("image") && null != descfall
&& descfall.equalsIgnoreCase(TRUE)) {
descClass = "descriptionFall";
try {
if (this.currentNode.hasNode(TEXT_NODE)) {
Node resTextNode = this.currentNode.getNode(TEXT_NODE);
descResText = textNode.getProperty("text_desc").getString();
}
} catch (PathNotFoundException e) {
LOGGER.error("PathNotFoundException ocurred while getting Current Node ", e);
}
}
}
This is my test class:
@BeforeAll
static void setUpBeforeClass() throws Exception {
aemContext.addModelsForClasses(TileModel.class);
aemContext.load().json("/com/domain/abc/slingmodels/Tile.json", "/content");
createBindings();
ResourceResolver resourceResolver = aemContext.resourceResolver();
Session session = resourceResolver.adaptTo(Session.class);
Node nodeContent = session.getRootNode().addNode("content");
Node nodeDam = nodeContent.addNode("Text");
tileModel = request.adaptTo(TileModel.class);
}
private static void createBindings() {
SlingBindings slingBindings = new SlingBindings();
Resource resource = aemContext.resourceResolver().getResource("/content/tile");
slingBindings.put(SlingBindings.RESOURCE, resource);
slingBindings.put(WCMBindings.PROPERTIES, resource.getValueMap());
request = new MockSlingHttpServletRequest(aemContext.resourceResolver(), aemContext.bundleContext());
request.setResource(resource);
Page homePage = aemContext.create().page("/content/abc/us/en/home");
slingBindings.put(WCMBindings.CURRENT_PAGE, homePage);
request.setAttribute(SlingBindings.class.getName(), slingBindings);
}