Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Facing issue while mocking CurrentNode

Avatar

Level 2

How to mock:

@inject
@source("script-bindings")
Node currentNode;

 

Not able to do through slingBindings as well as through session.

7 Replies

Avatar

Community Advisor

Hi,

You can inject resourecResolver and adapt to Node

resourceRsolver.adaptTo(Node.class)


Arun Patidar

Avatar

Level 2

I tried this:

ResourceResolver resourceResolver = aemContext.resourceResolver();
resourceResolver.adaptTo(Node.class);

 

But still not able to mock 

@inject
@source("script-bindings")
Node currentNode;

Avatar

Employee Advisor

Do you use the Sling Testing Framework?

Avatar

Level 2

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);
}