public class SomeClass extends WCMUsePojo {
@Override
public void activate() throws Exception {
SomeClassServiceImpl someServiceImpl = getSlingScriptHelper()
.getService(SomeClassServiceImpl.class);
if (null != someServiceImpl) {
someServiceImpl.someMethod(getRequest(), getResponse(), getCurrentPage());
}
}
}
On extending the class WCMUsepojo we are able to call methods like getRequest(), getResponse(), getCurrentPage(), getSlingScriptHelper()....
While writing the Junit using the Mockito I found it difficult to write the above way. Instead with the init method we accessed all the bindings like REQUEST, RESPONSE, CURRENT_PAGE..
SLING. The only thing we we have to do is initailize the bindings in the testActivate method
We need to Mock SlingScriptHelper,SlingBindings, the Impl class,SlingHttpServletRequest,SlingHttpServletResponse
please find the snippet
@Test
public void testActivate() throws Exception {
Bindings bindings = new SimpleBindings();
WCMBindings wcmBindings = new WCMBindings();
request = new MyRequest(slingRequest);
when(request.getAttribute(SlingBindings.class.getName())).thenReturn(slingBindings);
when(request.getAttribute(WCMBindings.class.getName())).thenReturn(wcmBindings);
when(slingBindings.getSling()).thenReturn(scriptHelper);
response = new MyResponse(slingResponse);
when(scriptHelper.getService(SomeClassServiceImpl.class)).thenReturn(someClassService);
bindings.put(SlingBindings.SLING, scriptHelper);
someClassService.someMethod(request, response, currentPage);
when(request.getResource()).thenReturn(resource);
when(resource.adaptTo(Page.class)).thenReturn(currentPage);
when(page.getPath()).thenReturn("/content");
bindings.put(SlingBindings.REQUEST, request);
bindings.put(SlingBindings.RESPONSE, response);
bindings.put(WCMBindings.CURRENT_PAGE, page);
SomeClass SomeClass = new SomeClass();
SomeClass.init(bindings);
}
/**
* The Class MyRequest.
*/
private class MyRequest extends SlingHttpServletRequestWrapper {
/**
* Instantiates a new my request.
*
* @param wrappedRequest
* the wrapped request
*/
public MyRequest(SlingHttpServletRequest wrappedRequest) {
super(wrappedRequest);
}
}
/**
* The Class MyResponse.
*/
private class MyResponse extends SlingHttpServletResponseWrapper {
/**
* Instantiates a new my response.
*
* @param wrappedRequest
* the wrapped response
*/
public MyResponse(SlingHttpServletResponse wrappedResponse) {
super(wrappedResponse);
}
}
Hope you find it helpful.
@Override
public void activate() throws Exception {
SomeClassServiceImpl someServiceImpl = getSlingScriptHelper()
.getService(SomeClassServiceImpl.class);
if (null != someServiceImpl) {
someServiceImpl.someMethod(getRequest(), getResponse(), getCurrentPage());
}
}
}
On extending the class WCMUsepojo we are able to call methods like getRequest(), getResponse(), getCurrentPage(), getSlingScriptHelper()....
While writing the Junit using the Mockito I found it difficult to write the above way. Instead with the init method we accessed all the bindings like REQUEST, RESPONSE, CURRENT_PAGE..
SLING. The only thing we we have to do is initailize the bindings in the testActivate method
We need to Mock SlingScriptHelper,SlingBindings, the Impl class,SlingHttpServletRequest,SlingHttpServletResponse
please find the snippet
@Test
public void testActivate() throws Exception {
Bindings bindings = new SimpleBindings();
WCMBindings wcmBindings = new WCMBindings();
request = new MyRequest(slingRequest);
when(request.getAttribute(SlingBindings.class.getName())).thenReturn(slingBindings);
when(request.getAttribute(WCMBindings.class.getName())).thenReturn(wcmBindings);
when(slingBindings.getSling()).thenReturn(scriptHelper);
response = new MyResponse(slingResponse);
when(scriptHelper.getService(SomeClassServiceImpl.class)).thenReturn(someClassService);
bindings.put(SlingBindings.SLING, scriptHelper);
someClassService.someMethod(request, response, currentPage);
when(request.getResource()).thenReturn(resource);
when(resource.adaptTo(Page.class)).thenReturn(currentPage);
when(page.getPath()).thenReturn("/content");
bindings.put(SlingBindings.REQUEST, request);
bindings.put(SlingBindings.RESPONSE, response);
bindings.put(WCMBindings.CURRENT_PAGE, page);
SomeClass SomeClass = new SomeClass();
SomeClass.init(bindings);
}
/**
* The Class MyRequest.
*/
private class MyRequest extends SlingHttpServletRequestWrapper {
/**
* Instantiates a new my request.
*
* @param wrappedRequest
* the wrapped request
*/
public MyRequest(SlingHttpServletRequest wrappedRequest) {
super(wrappedRequest);
}
}
/**
* The Class MyResponse.
*/
private class MyResponse extends SlingHttpServletResponseWrapper {
/**
* Instantiates a new my response.
*
* @param wrappedRequest
* the wrapped response
*/
public MyResponse(SlingHttpServletResponse wrappedResponse) {
super(wrappedResponse);
}
}
Hope you find it helpful.