need help in writing junit test case for below code. | Community
Skip to main content
Level 3
March 25, 2025
Solved

need help in writing junit test case for below code.

  • March 25, 2025
  • 6 replies
  • 915 views

public class CanonicalURL extends WCMUsePojo {

public LinkCreator linkCreatorUtil;

public String path;

 

@9944223

public void activate() throws Exception {

linkCreatorUtil = getSlingScriptHelper().getService(LinkCreator.class);

path = get("path", String.class);

if (path == null) {

path = getCurrentPage().getPath();

}

}
can some one help in writing junit test case for this?
because mocking getSlingScriptHelper() is not working.

lenient().when(wcmUsePojo.getSlingScriptHelper()).thenReturn(slingScriptHelper);

lenient().when(slingScriptHelper.getService(LinkCreator.class)).thenReturn(linkCreator);

 

Best answer by Mahedi_Sabuj

You can mock getSlingScriptHelper like below:

@Mock private Bindings bindings; @Mock private SlingScriptHelper slingScriptHelper; @BeforeEach void setUp() { Mockito.when(bindings.get("sling")).thenReturn(slingScriptHelper); Mockito.when(slingScriptHelper.getService(LinkCreator.class)) .thenReturn(linkCreator); canonicalURL.init(bindings); }

 

6 replies

Level 3
March 25, 2025

java.lang.NullPointerException: Cannot invoke "javax.script.Bindings.get(Object)" because "this.bindings" is null
at com.adobe.cq.sightly.WCMUsePojo.get(WCMUsePojo.java:110)
at com.adobe.cq.sightly.WCMUsePojo.getSlingScriptHelper(WCMUsePojo.java:283)
at com.morganstanley.mscommons.core.CanonicalURLTest.testActivate_WithPath(CanonicalURLTest.java:58)


error im getting while mocking

arunpatidar
Community Advisor
Community Advisor
March 25, 2025
Mahedi_Sabuj
Community Advisor
Mahedi_SabujCommunity AdvisorAccepted solution
Community Advisor
March 25, 2025

You can mock getSlingScriptHelper like below:

@Mock private Bindings bindings; @Mock private SlingScriptHelper slingScriptHelper; @BeforeEach void setUp() { Mockito.when(bindings.get("sling")).thenReturn(slingScriptHelper); Mockito.when(slingScriptHelper.getService(LinkCreator.class)) .thenReturn(linkCreator); canonicalURL.init(bindings); }

 

Mahedi Sabuj
konstantyn_diachenko
Community Advisor
Community Advisor
March 25, 2025

Hi @sudarshanv1 ,

 

Rely on AEM Context while writing unit tests. https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/project-archetype/unit-testing 

@ExtendWith({ AemContextExtension.class, MockitoExtension.class }) class YourTest { private final AemContext aemContext = new AemContext(); @BeforeEach void init() { SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName()); CanonicalURL model = new CanonicalURL(); model.init(bindings); } }

 Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
kautuk_sahni
Community Manager
Community Manager
April 1, 2025

@sudarshanv1 Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!

Kautuk Sahni