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?
Solved! Go to Solution.
Views
Replies
Total Likes
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
}
Views
Replies
Total Likes
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
}
Views
Replies
Total Likes
Could you please brief out this line
aemContext.load().json("/json-import-samples/content.json", "/content/sih");
Views
Replies
Total Likes
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>
Views
Replies
Total Likes
Views
Likes
Replies