Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

wcmusepojo aem context junit

Avatar

Level 2

    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?

1 Accepted Solution

Avatar

Correct answer by
Level 2

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

}

View solution in original post

0 Replies

Avatar

Correct answer by
Level 2

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

}

Avatar

Level 2

Could you please brief out this line

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

Avatar

Level 2

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>