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

srinivas_chann1

22-06-2020

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


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

Accepted Solutions (0)

Answers (2)

Answers (2)

srinivas_chann1

22-06-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