Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

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

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@ReshmaRaj Below is the sample code 

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

 

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

@ReshmaRaj Below is the sample code 

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

 

Avatar

Level 2

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 ?

Avatar

Community Advisor

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-aemcontextexten... 

 

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