Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 8

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

 

@Override
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();


@Deleted Account
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());

 

}

@test
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>

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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/co...)

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. 

View solution in original post

9 Replies

Avatar

Level 8

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 

Avatar

Correct answer by
Community Advisor

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/co...)

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. 

Avatar

Level 8

Thanks for the input.

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

Still facing the same issue

 

Thanks

Avatar

Community Advisor

Hi @srinivas_chann1,

No worries. Could you please share details of CI tool used if it is ok to share.

Avatar

Level 8

Hi ,Thanks for looking into .Could you please help if possible without more details on CI tool as I see when  i see  JMockit  then CI is able to get those current page and resource without any issue. Not sure when i am doing binding with    MockitoJUnitRunner  then it is not picking the binding values,till i do something like 

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

 

@RunWith(JMockit.class)

 

 


import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.zebra.web.base.AbstractComponent;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;

@RunWith(JMockit.class)
public class trialMock(){

@Mocked
public WCMUsePojo wcmUsePojo;

@Mocked
protected Resource resource;

@Mocked
public WCMUsePojo wcmUsePojo;

@Mocked
protected Page currentPage;

protected void init() {
new MockUp<trialMock>() {
@Mock
public WCMUsePojo getWcmUsePojo() {
return wcmUsePojo;
}

@Mock
public Page getCurrentPage() {
return currentPage;
}

@Mock
public Resource getResource() {
return resource;
}


}

 

Avatar

Community Advisor

Hi @srinivas_chann1,

We can narrow down to CI tool issue and not test class issue by executing the build in local command prompt instead of from IDE. Please check and let know the result.

If it is through in local command prompt build, then it should be purely at CI tool level which lets us to think of various possible causes right from maven version to the conflicts in dependency version.

 

Outside this, can you confirm if the mock JSON in class path is committed to GitHub/ your version control that is configured in CI tool

 

Avatar

Level 8

Thanks for looking into it.

I see that local build on cmd is working properly like IDE no issues only when the build is ran on CI tool then binding fails and i need to have check for binding.for CI tool

 

Thanks

Avatar

Community Advisor

Hi @srinivas_chann1,

No issues, Is it able to get the mock JSON available in class path in CI tool.