MockitoJUnitRunner bind for WCMUsePojo fails for currentPage and resource in CI (continuous-integration tool) | Community
Skip to main content
srinivas_chann1
Level 7
June 22, 2020
Solved

MockitoJUnitRunner bind for WCMUsePojo fails for currentPage and resource in CI (continuous-integration tool)

  • June 22, 2020
  • 3 replies
  • 2222 views

Hi,

I am using MockitoJUnitRunner  for Junit .

 

As per the below code the bind is not happening in the WCMUsePojo component due to which it shows nullpointer expection for both currentPage and resource that is present in activate method of the component ,but the bind works for request and response.

 

Also it works in my intellji but the same does not work when same is run in CI tool what could be the issue,with the same code

 

@9944223
public void activate() throws Exception {
//does not work null pointer
currentPage = getCurrentPage();
resource = getResource();

//picks up
slingHttpServletRequest = getRequest();
slingHttpServletResponse = getResponse();

}

 

//My code for the component:-

 

 


@RunWith(MockitoJUnitRunner.Silent.class)
public class BaseMockTest {

private final TestClass testClass = new TestClass();


@1227241
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

@Mock
protected MockSlingHttpServletRequest req;

@Mock
protected MockSlingHttpServletResponse res;

@Mock
protected Bindings bindings;

 

public void initialize(Page page, Resource resource){

when(bindings.get(SlingBindings.REQUEST)).thenReturn(req);
//response
when(bindings.get(SlingBindings.RESPONSE)).thenReturn(res);
//getCurrentPage
when(bindings.get(WCMBindings.CURRENT_PAGE)).thenReturn(page);
when(bindings.get(WCMBindings.RESOURCE_PAGE)).thenReturn(page);
//getPageProperties
when(bindings.get(WCMBindings.PAGE_PROPERTIES)).thenReturn(resource.getValueMap());
//properties
when(bindings.get(WCMBindings.PROPERTIES)).thenReturn(resource.getValueMap());
//getResource
when(bindings.get(SlingBindings.RESOURCE)).thenReturn(context.request().getResource());
//getslingScriptHelper
when(bindings.get(SlingBindings.SLING)).thenReturn(context.slingScriptHelper());
//getInheritedProperties
when(bindings.get(WCMBindings.INHERITED_PAGE_PROPERTIES)).thenReturn(resource.getValueMap());

 

}

@2785667
public void test() {

req = context.request();
res = context.response();
context.load().json("/metadata.json",
"/content/abc/en/page");
Resource resource = context.create().
resource("/content/abc/en/page/jcr:content/par/nodetest");
context.request().setPathInfo("/content/abc/en/page");
page=context.create().page("/content/abc/en/page");

initialize(page, resource);
testClass.init(bindings);
}}

 

//pom 

<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit4</artifactId>
<version>2.7.2</version>
<scope>test</scope>
</dependency>

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 Vijayalakshmi_S

Hi @srinivas_chann1,

Please confirm if my below understanding is correct.

getCurrentPage() and getResource() works fine in IDE but not in CI tool. 

 

For getCurrentPage() dummy implementation, try using the below and see if it helps.

Looks like WCMBindings.CURRENT_PAGE is deprecated.(https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/adobe/cq/sightly/WCMBindings.html)

when(bindings.get(WCMBindingsConstants.NAME_CURRENT_PAGE)).thenReturn(page);

 For getResource(), the dummy implementation you have provided retrieves a resource from request. 

Given that you have created a resource in test method, try the following

when(bindings.get(SlingBindings.RESOURCE)).thenReturn(context.currentResource(resource));// resource in the argument is the one that you pass from test method via initialize(page, resource)

Outside the issue statement, noticed that you have used @Mock for MockSlingHttpServletRequest.

Explicit annotation is not required as MockSlingHttpServletRequest itself is a mock request object. 

3 replies

srinivas_chann1
Level 7
June 23, 2020

I nocticed that when using CI tool it doing the binfding that is present in WCMUsePojo and works when we do 

 

currentPage = getCurrentPage();
if(null==currentPage ){
currentPage =(Page)this.get("currentPage", Page.class);
}

But i would not want to add this code in the component as i would then to add this code in each component.Please let me know how this could be solved with doing this for CI tool build.

 

Note:- There is no change required when using IDE intelij 

srinivas_chann1
Level 7
June 23, 2020

Any inputs from anyone on this

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
July 20, 2020

Hi @srinivas_chann1,

Please confirm if my below understanding is correct.

getCurrentPage() and getResource() works fine in IDE but not in CI tool. 

 

For getCurrentPage() dummy implementation, try using the below and see if it helps.

Looks like WCMBindings.CURRENT_PAGE is deprecated.(https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/adobe/cq/sightly/WCMBindings.html)

when(bindings.get(WCMBindingsConstants.NAME_CURRENT_PAGE)).thenReturn(page);

 For getResource(), the dummy implementation you have provided retrieves a resource from request. 

Given that you have created a resource in test method, try the following

when(bindings.get(SlingBindings.RESOURCE)).thenReturn(context.currentResource(resource));// resource in the argument is the one that you pass from test method via initialize(page, resource)

Outside the issue statement, noticed that you have used @Mock for MockSlingHttpServletRequest.

Explicit annotation is not required as MockSlingHttpServletRequest itself is a mock request object. 

srinivas_chann1
Level 7
July 20, 2020

Thanks for the input.

Yes getCurrentPage() and getResource() works fine in IDE but not in CI tool. 

Still facing the same issue

 

Thanks