Expand my Community achievements bar.

SOLVED

Junit mock for Pagemanager getPage

Avatar

Level 2

Hi All,

I need to mock this statement in my class.

PageManager pageManager = resolver.adaptTo(PageManager.class);

pageManager.getPage("/test/sample");

 

So in my junit class, I have this code,

lenient().when(pageManager.getPage("/test/sample")).thenReturn(context.create().page("/test/sample");

 

while debugging i could see that

In java code the page manager object is

io.wcm.testing.mock.aem.MockPageManager@58882a93

 

In junit class

Mock for PageManager, hashCode: 749625255

 

Is that the reason, why pageManager.getPage("/test/sample") returns null inspite of having a create page in junit?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ArchR 
Can you check a below sample code

 

 

import com.adobe.cq.wcm.api.Page;
import com.adobe.cq.wcm.api.PageManager;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class PageManagerTest {

    @Rule
    public final AemContext context = new AemContext();

    private PageManager pageManager;

    @Before
    public void setUp() {
        // Retrieve the PageManager from the AemContext
        pageManager = context.pageManager();
    }

    @Test
    public void testGetPage() {
        // Create a sample content structure for testing
        context.create().page("/content/sample", "/content/sample/page1");

        // Get the page using the PageManager API
        Page page = pageManager.getPage("/content/sample/page1");

        // Assert that the page is not null
        assertNotNull("Page should not be null", page);
    }
}

 



Arun Patidar

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hi @ArchR 
Can you check a below sample code

 

 

import com.adobe.cq.wcm.api.Page;
import com.adobe.cq.wcm.api.PageManager;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class PageManagerTest {

    @Rule
    public final AemContext context = new AemContext();

    private PageManager pageManager;

    @Before
    public void setUp() {
        // Retrieve the PageManager from the AemContext
        pageManager = context.pageManager();
    }

    @Test
    public void testGetPage() {
        // Create a sample content structure for testing
        context.create().page("/content/sample", "/content/sample/page1");

        // Get the page using the PageManager API
        Page page = pageManager.getPage("/content/sample/page1");

        // Assert that the page is not null
        assertNotNull("Page should not be null", page);
    }
}

 



Arun Patidar

Avatar

Community Advisor

Hi @ArchR 

  1. The mock setup is incorrect: Double-check your mock setup to ensure that it is correctly configured to return the created page. Make sure that the context.create().page("/test/sample") is returning a valid page object.

  2. The method invocation is different: Verify that the pageManager.getPage("/test/sample") method is being invoked with the exact same arguments as in the mock setup. If there are any differences, the mock will not match and return null.

  3. The mock object is not being used: Ensure that the mock object is being injected or used in the class under test. If the actual PageManager object is being used instead of the mock, the mock setup will not be applied.



Avatar

Community Advisor

By examining your code, the straightforward explanation for why it's returning null is because the conditions for this mock are not being fulfilled:

lenient().when(pageManager.getPage("/test/sample")).thenReturn(context.create().page("/test/sample");

You need to check during execution (debugging):

1. If the `pageManager` object is the same object returned from `resolver.adaptTo(PageManager.class)`. Ensure you are using `registerAdapter()` to return your mocked `PageManager` object. You can find information on how to do this in the following blog: https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/.

2. Verify that the `getPage()` method in your execution is using the parameters "/test/sample" to invoke the method.

 

Once these conditions are met, your stubbing will occur and you won't have null anymore

 

Hope this helps.

 

Hope this helps.



Esteban Bustamante

Avatar

Level 2

Inspite of having registerAdapter,  the pagemanger mock object is not getting picked. 

 

the same registerAdapter returns mock object when I adapt ResourceResolver to Page or QueryBuilder. Only issue i see is with PageManager

 

Below is the difference i expect a mock object like the same one in my junit test class, so that i can return a page from there.

 

In java code the page manager object is

io.wcm.testing.mock.aem.MockPageManager@58882a93

 

In junit class

Mock for PageManager, hashCode: 749625255

Avatar

Administrator

@ArchR Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni