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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Unit Test for Servlet registered using path.

Avatar

Level 3

Hi all, 

 

I am new to Unit Testing in aem and I have a requirement to write test cases for a servlet registered using path and not resourceType.

 

There is no reference for the same. All the examples are based on servlet using resourceType.

 Is there any documentation for the same?.

 

This is my servlet for the refrence:

 

import javax.servlet.Servlet;


import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.my.site.core.services.InstafeedService;

@Component(service = Servlet.class, property = {
    Constants.SERVICE_DESCRIPTION + "=HTTP servlet",
    "sling.servlet.methods=" + HttpConstants.METHOD_GET,
    "sling.servlet.paths=" + "/bin/servlets/insta-feed"
})
public class InstafeedServlet extends SlingAllMethodsServlet {
	
	
	/**
	 * Logger
	 */
	private static final Logger log = LoggerFactory.getLogger(InstafeedServlet.class);
	
	@Reference
	private InstafeedService httpService;
	
	/**
	 * Overridden doGet() method
	 */
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
		try {	
            
			/**
             * Printing the json response on the browser
             */
            response.getWriter().println(httpService.getInstaFeed());
		} catch (Exception e) {
			log.error("error in getting the insta post >>> "+e.getMessage());
		}
	}
	
}
Also as in models we use addModelsForClasses 

Is there something similar for servlet?

 

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 6

There isn't much difference between unit testing the servlet registered with path and resourceType. The only difference would be how you register the servlet with the path in the unit test - context.request().setPathInfo("/bin/servlets/insta-feed");

 

You can refer the below code for reference  

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(AemContextExtension.class)
class InstafeedServletTest {

    AemContext context = new AemContext();
    InstafeedServlet instafeedServlet;
    @BeforeEach
    public void setUp() throws Exception{

        context.registerInjectActivateService(InstafeedService.class);
        instafeedServlet = new InstafeedServlet();
        
        // this is how you set the path to the servlet in test class
        context.request().setPathInfo("/bin/servlets/insta-feed");
    }
    
    @Test
    void testFeed() {
        instafeedServlet.doGet(context.request(), context.response());
        assertEquals("expected-response", context.response().toString()); 
    }

}

 

View solution in original post

3 Replies

Avatar

Correct answer by
Level 6

There isn't much difference between unit testing the servlet registered with path and resourceType. The only difference would be how you register the servlet with the path in the unit test - context.request().setPathInfo("/bin/servlets/insta-feed");

 

You can refer the below code for reference  

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(AemContextExtension.class)
class InstafeedServletTest {

    AemContext context = new AemContext();
    InstafeedServlet instafeedServlet;
    @BeforeEach
    public void setUp() throws Exception{

        context.registerInjectActivateService(InstafeedService.class);
        instafeedServlet = new InstafeedServlet();
        
        // this is how you set the path to the servlet in test class
        context.request().setPathInfo("/bin/servlets/insta-feed");
    }
    
    @Test
    void testFeed() {
        instafeedServlet.doGet(context.request(), context.response());
        assertEquals("expected-response", context.response().toString()); 
    }

}

 

Avatar

Level 3

My servlet returns json output so I want to know how to create mock data for the same.Should I create a json file and the include it using context.load().json()?