Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

Junits services and servlets

sampath_kumar_g
Level 2
Level 2

package com.combotool.community.core.services;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.combotool.community.constants.ComboConstants;
import com.day.cq.commons.Filter;
import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.WCMException;

public class GuidelinesServiceTest {
@InjectMocks

GuidelinesService guidelinesService;
// HeLinksService heLinksService;

@Mock
ResourceResolverFactory resourceResolverFactory;
@Mock
ResourceResolver resourceResolver;
@Mock
Session session;
@Mock
PageManager pageManager;
@Mock
SlingHttpServletRequest request;
@Mock
SlingHttpServletResponse response;
@Mock
Page page1;
@Mock
Iterator<Page> iterator;
@Mock
Resource resource;
@Mock
Node node;
@Mock
Node subNode;
@Mock
PrintWriter print;
@Mock
Page childPage;
@Mock
ValueMap childProps;
@Mock
Property docTitle;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@SuppressWarnings("unchecked")
@test
public void test() throws LoginException, RepositoryException, JSONException, IOException, ParseException {

Mockito.when(resourceResolverFactory.getServiceResourceResolver(Mockito.anyMap())).thenReturn(resourceResolver);
Mockito.when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
Mockito.when(resourceResolver.adaptTo(PageManager.class)).thenReturn(pageManager);
Mockito.when(pageManager.getPage(ComboConstants.TRIAGE_GUIDELINES_SERVICE_URL)).thenReturn(page1);

Mockito.when(page1.listChildren()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true);
Mockito.when(iterator.next()).thenReturn(childPage);

Mockito.when(childPage.getContentResource()).thenReturn(resource);
Mockito.when(resource.adaptTo(Node.class)).thenReturn(node);
Mockito.when(node.hasNode("guideline")).thenReturn(true);
Mockito.when(node.hasProperty(Mockito.anyString())).thenReturn(true);
Mockito.when(childPage.getProperties()).thenReturn(childProps);
Mockito.when(node.getNode("guideline")).thenReturn(subNode);
Mockito.when(node.hasProperty("title")).thenReturn(true);
Mockito.when(node.getProperty("title")).thenReturn(docTitle);
Mockito.when(subNode.hasProperty("title")).thenReturn(true);
Mockito.when(subNode.getProperty("title")).thenReturn(docTitle);
//Mockito.when(childProps.get(Mockito.anyString(), Mockito.anyString())).thenReturn("2018-01-09T11:11:02.0+03:00");
Mockito.when(subNode.hasProperty("gender")).thenReturn(true);
Mockito.when(subNode.getProperty("gender")).thenReturn(docTitle);

JSONObject guidelineJsonObj = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
guidelineJsonObj.put("doctitle", docTitle);
jsonArray.put(guidelineJsonObj);
jsonObj.put("PageDetails", jsonArray);

Mockito.when(response.getWriter()).thenReturn(print);
guidelinesService.processRequest(request, response);
}
}

Servlet :

package com.combotool.community.core.servlets;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.commons.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.combotool.community.core.services.DbService;

public class DbServletTest {

@InjectMocks
DbServlet dbObj;

@Mock
DbService dbService;
@Mock
PrintWriter print;
@Mock
SlingHttpServletRequest request;
@Mock
SlingHttpServletResponse reponse;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@test
public void testdoGet() throws IOException, ServletException {
JSONObject obj = new JSONObject();
Mockito.when(reponse.getWriter()).thenReturn(print);
Mockito.when(dbService.getDbObj()).thenReturn(obj);
dbObj.doPost(request, reponse);
}


}

2 Replies
snbaem
Community Advisor
Community Advisor

Hi @sampath_kumar_g 

If there no question here and rather an intent to demonstrate usage of Junit services and servlets, please consider adding this as a discussion.