Expand my Community achievements bar.

SOLVED

junit test cases in aem

Avatar

Former Community Member

I tried to written test cases in below controller class but facing null pointer exception

public class SiteXMLCreatorController extends WCMUse implements Controller

{

PageManager pm = getPageManager();

Page parentProvided = pm.getPage(getProperties().get("sectionparent",
                String.class));

if (parentProvided != null) {int depth_selected = Integer.parseInt((String) getProperties().get("depth"));

}

I written test cases

public class SiteXMLCreatorControllerTest

{


    SiteXMLCreatorController underTest;
    
    @Mock
    Page P_MOCK;
    
    @Mock
    PageManager PAGE_MOCK;
    
    @Mock
    ValueMap properties;
    
    @Mock
    WCMUse WCMUSE_MOCK;
    
    @Mock
    ResourceResolver resourceResolver;
 
    @Before
    public void setUp() throws Exception {
        underTest=new SiteXMLCreatorController();
        //when(underTest.getPageManager()).thenReturn(PAGE_MOCK);
    } 

@Test
    public void testGetSiteMap() throws Exception
    {
        when(WCMUSE_MOCK.getPageManager()).thenReturn(PAGE_MOCK);
        when(PAGE_MOCK.getPage("sectionparent")).thenReturn(P_MOCK);
        when(PAGE_MOCK.getContainingPage("/content/geometrixx/en").getPath()).thenReturn(P_MOCK);

}

last line of code i getting error. Please let me know how resolve it.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

What is your implementation of PageManager? If you don't implement it yourself, mockito mocks return null by default. so "getContainingPage()" will always return null, thus running in an NPE.

kind regards,
Jörg

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

Hi,

What is your implementation of PageManager? If you don't implement it yourself, mockito mocks return null by default. so "getContainingPage()" will always return null, thus running in an NPE.

kind regards,
Jörg