wcmusepojo aem context junit | Community
Skip to main content
maheswarim77225
New Participant
May 30, 2017
Solved

wcmusepojo aem context junit

  • May 30, 2017
  • 3 replies
  • 5690 views

    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.
Best answer by ekaggarw

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

New Participant
September 12, 2018

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>

New Participant
September 12, 2018

Could you please brief out this line

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

ekaggarwAccepted solution
New Participant
July 31, 2017

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

}