junit test cases in aem | Community
Skip to main content
October 16, 2015
Solved

junit test cases in aem

  • October 16, 2015
  • 1 reply
  • 1271 views

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.

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 joerghoh

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

1 reply

joerghoh
joerghohAccepted solution
Employee
October 16, 2015

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