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
Solved! Go to Solution.
Views
Replies
Total Likes
@ReshmaRaj Below is the sample code
private final AemContext aemContext = new AemContext();
@Mock
PageManager pageManager;
pageManager = aemContext.pageManager();
@ReshmaRaj Below is the sample code
private final AemContext aemContext = new AemContext();
@Mock
PageManager pageManager;
pageManager = aemContext.pageManager();
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 ?
Hi @ReshmaRaj ,
You can try the code sample given by @Jagadeesh_Prakash.
Also, I'm adding an old community post here for future reference.
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
Views
Likes
Replies