Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

Junit | slingRequestProcessor.process returning empty string


Level 4
Level 4

Hi All,

Getting empty string for baos.toString("UTF-8") statement. Performing below steps to invoke a workflow process passing mock object.

1. Creating mock objects for request, response and resource resolver.
2. Passing mock values, as below
3. successfully getting all the mock objects in workflow process class.

Invoking getJSONFromPSPPPage method internally from execute method, but getting empty String for below statement

String baosString = baos.toString("UTF-8");

Any thoughts or Suggestion please.

Junit class --

MockSlingHttpServletRequest request;
MockSlingHttpServletResponse response;
private Replicator replicator;
private RequestResponseFactory requestResponseFactory;
private SlingRequestProcessor requestProcessor;

public void setup()
{ response = context.response();
request = new MockSlingHttpServletRequest(resourceResolverMock, context.bundleContext());
context.registerService(RequestResponseFactory.class, requestResponseFactory);
PrivateAccessor.setField(process, "requestResponseFactory", requestResponseFactory);
context.registerService(SlingRequestProcessor.class, requestProcessor);
PrivateAccessor.setField(process, "requestProcessor", requestProcessor);

void test(){
String path = "/content/page/us";
request = new MockSlingHttpServletRequest(resourceResolverMock, context.bundleContext());
Mockito.when(requestResponseFactory.createRequest(Mockito.anyString(), Mockito.anyString()))

process.execute(wfWorkItemMock, wfSessionMock, metaData);

Inside Workflow Process class

private String getJSONFromPSPPPage(String path, ResourceResolver resourceResolver) {
String psppJSON = null;
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

String psppPath = path + "/"+ JcrConstants.JCR_CONTENT + PSPP_JSON_EXTN;
logger.info("PSPP path for url : {}", psppPath);

HttpServletRequest req = requestResponseFactory.createRequest("GET", psppPath);
HttpServletResponse resp = requestResponseFactory.createResponse(baos);

requestProcessor.processRequest(req, resp, resourceResolver);

String baosString = baos.toString("UTF-8");
if (StringUtils.equals(resp.getContentType(), "application/json")) {
psppJSON = baosString;

logger.info("PSPP Json : {}", psppJSON);

} catch (Exception ex) {
logger.error("Error in creating json from PSPP", ex);

return psppJSON;



Topics help categorize Community content and increase your ability to discover relevant content.

2 Replies


Community Advisor

Hi @nj2 ,


I do not see any value assignment to baos variable inside getJSONFromPSPPPage method.


ByteArrayOutputStream baos = new ByteArrayOutputStrea


Above will give you empty baos object, which you are trying to convert to String with UTF-8 encoding so it will be empty too. So it it does not look like unit test issue.


Level 4
Level 4
Thank you @Ritesh_Mittal for your reply, as per my understanding requestProcessor.processRequest(req, resp, resourceResolver); statment will assign the generated response to baos object isn't it. getJSONFromPSPPPage method code is running perfectly fine but somehow not for Junit. Can you please elaborate your point, I might be missing something. Thank you one more time, appreciate all your help.