Can someone suggest how to mock JcrUtil.createPath(destinationPath, JcrConstants.NT_FOLDER, session) using AemContextExtension ? | Community
Skip to main content
Level 2
January 24, 2023
Solved

Can someone suggest how to mock JcrUtil.createPath(destinationPath, JcrConstants.NT_FOLDER, session) using AemContextExtension ?

  • January 24, 2023
  • 2 replies
  • 2521 views

I tried using context.pageManager().create("pagePath", "pageName", "template", "pageTitle", true);

but pageManager is always null. Please give your suggestions on how to mock JcrUtil methods using Junit5

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jagadeesh_Prakash

@reshmaraj Below is the sample code 

private final AemContext aemContext = new AemContext();
@Mock
PageManager pageManager;
pageManager = aemContext.pageManager();

 

2 replies

Jagadeesh_Prakash
Community Advisor
Jagadeesh_PrakashCommunity AdvisorAccepted solution
Community Advisor
January 24, 2023

@reshmaraj Below is the sample code 

private final AemContext aemContext = new AemContext();
@Mock
PageManager pageManager;
pageManager = aemContext.pageManager();

 

ReshmaRajAuthor
Level 2
January 27, 2023

Thanks for your quick response. Even after following this, pagemanager object is still null as shown below.

 

java.lang.RuntimeException: No page manager.

at io.wcm.testing.mock.aem.builder.ContentBuilder.page(ContentBuilder.java:150)

at io.wcm.testing.mock.aem.builder.ContentBuilder.page(ContentBuilder.java:106)

 

Is there anything else that I should look for ?

Aditya_Chabuku
Community Advisor
Community Advisor
January 24, 2023

Hi @reshmaraj ,

You can try the code sample given by @jagadeesh_prakash.

Also, I'm adding an old community post here for future reference.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-with-aemcontextextension/m-p/360751/highlight/true#M22071 

 

import com.adobe.cq.commerce.common.ValueMapDecorator; import com.day.cq.wcm.api.Page; import com.google.common.collect.ImmutableMap; import io.wcm.testing.mock.aem.junit.AemContext; import org.apache.sling.api.resource.Resource; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import static junitx.framework.Assert.assertEquals; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class HeaderTest { @Rule public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK); // the context.resourceResolver() is auto injected by the AemContext, cannot be mocked. // ResourceResolver resolver; // mocking the global AEM object "currentPage". // variable does not need to match the variables in the underTest.class @Mock private Page currentPage; // injects all the mocks into the tested object. @InjectMocks private Header underTest; @Test public void itShouldReturnTheCorrectSecretCharWhenResourceExist() { // using the AEM context to create an AEM resource in the context, to set properties for the resource. // the resource path can be anything made up. Resource headerResourceContext = context.create().resource("/content/sourcedcode/home/jcr:content/header", new ValueMapDecorator(ImmutableMap.<String, Object> of( "contactUsPath", "/content/sourcedcode/contact", "anotherProperty", "example"))); // create mock page, resolved by the resolver. context.create().page("/content/sourcedcode/contact", "", ImmutableMap.<String, Object>builder() .put("jcr:title", "Contact Us Page") .build()); } }

Thanks,

Aditya Chabuku

Thanks,Aditya Chabuku