Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

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.

2 Replies

Avatar

Community Advisor

Hi,

You can inject resourecResolver and adapt to Node

resourceRsolver.adaptTo(Node.class)

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

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

 

 

 

The ultimate experience is back.

Join us in Vegas to build skills, learn from the world's top brands, and be inspired.

Register Now