wcmusepojo aem context junit | Community
Skip to main content
maheswarim77225
Level 2
May 30, 2017 7:06 AM UTC
Solved

wcmusepojo aem context junit

    we are trying to unit test our wcmuse pojo class with the AEMContext api. But getting null pointer exception in the line where we use get() in the activate method. Has anyone faced similar situation. I am seeing reference links which talks about testing WCMUsePojo using mocking frameworks like Mockito. But is it possible to test using AEMContext api?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
ekaggarwAccepted solution
Level 2
Jul 31, 2017 9:41 AM UTC

Yes we can test WCMUsePojo using AEM Mocks. To Mock the get methods of WCMUsePojo you need to mock the Bindings. Take a look of below sample class.

public class SampleTest {

   @Rule
   public AemContext aemContext = new AemContext();

  Sample obj = new Sample();

  Resource resource = mock(Resource.class);

  SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);

  Bindings bindings = mock(Bindings.class);

   @Before
   public void setUp() throws Exception {

   aemContext.load().json("/json-import-samples/content.json", "/content/sih");

   when(bindings.get("request")).thenReturn(request);

   when(request.getResourceResolver()).thenReturn(aemContext.resourceResolver());

   when(bindings.get("inheritedPageProperties")).thenReturn(aemContext.resourceResolver().getResource("/content/sih/en/jcr:content").getValueMap());

   when(bindings.get("properties")).thenReturn(aemContext.resourceResolver().getResource("/content/sih/en/jcr:content/header/topheader").getValueMap());

  }

@Test

//Write test method

}

3 replies

Level 2
Sep 12, 2018 7:24 AM UTC

Hi Bhavyas,

The content.json provided under the path "/json-import-samples" will be treated as the jcr:content of "/content/sih" page path. It could be your project path as well. Example : /content/<project-name>

Translate
Level 2
Sep 12, 2018 7:13 AM UTC

Could you please brief out this line

  aemContext.load().json("/json-import-samples/content.json", "/content/sih");

Translate
ekaggarwAccepted solution
Level 2
Jul 31, 2017 9:41 AM UTC

Yes we can test WCMUsePojo using AEM Mocks. To Mock the get methods of WCMUsePojo you need to mock the Bindings. Take a look of below sample class.

public class SampleTest {

   @Rule
   public AemContext aemContext = new AemContext();

  Sample obj = new Sample();

  Resource resource = mock(Resource.class);

  SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);

  Bindings bindings = mock(Bindings.class);

   @Before
   public void setUp() throws Exception {

   aemContext.load().json("/json-import-samples/content.json", "/content/sih");

   when(bindings.get("request")).thenReturn(request);

   when(request.getResourceResolver()).thenReturn(aemContext.resourceResolver());

   when(bindings.get("inheritedPageProperties")).thenReturn(aemContext.resourceResolver().getResource("/content/sih/en/jcr:content").getValueMap());

   when(bindings.get("properties")).thenReturn(aemContext.resourceResolver().getResource("/content/sih/en/jcr:content/header/topheader").getValueMap());

  }

@Test

//Write test method

}

Translate