Unit test for post sling servlet aem 6.5

722 views Asked by At

I have the following POST servlet that adds new node under certain resource with parameters(name and last nam) from the request:

@Component(
        service = Servlet.class,
        property = {
                "sling.servlet.paths=/bin/createuser",
                "sling.servlet.methods=" + HttpConstants.METHOD_POST
        })
public class CreateNodeServlet extends SlingAllMethodsServlet {

    /**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger(CreateNodeServlet.class);

    @Override
    protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws IOException {
        log.info("Inside CreateNodeServlet");
        ResourceResolver resourceResolver = req.getResourceResolver();
        final Resource resource = resourceResolver.getResource("/content/test/us/en");
        String name = req.getParameter("name");
        String lastname = req.getParameter("lastname");
        log.info("name :{}",name);
        log.info("lastname :{}",lastname);

        Node node = resource.adaptTo(Node.class);
        try {
            log.info("Node {}", node.getName() );

            Node newNode = node.addNode(name+lastname, "nt:unstructured");
            newNode.setProperty("name", name);
            newNode.setProperty("lastname", lastname);

            resourceResolver.commit();
        } catch (RepositoryException e) {
            e.printStackTrace();
        } catch (PersistenceException e) {
            e.printStackTrace();
        }
        resp.setStatus(200);
        resp.getWriter().write("Simple Post Test");
    }
}

I tried creating unit test for this I got this so far:

@ExtendWith(AemContextExtension.class)
class CreateNodeServletTest {

    private final AemContext context = new AemContext();

    private CreateNodeServlet createNodeServlet = new CreateNodeServlet();

    @Test
    void doPost() throws IOException, JSONException {

        context.currentPage(context.pageManager().getPage("/bin/createuser"));
        context.currentResource(context.resourceResolver().getResource("/bin/createuser"));
        context.requestPathInfo().setResourcePath("/bin/createuser");

        MockSlingHttpServletRequest request = context.request();
        MockSlingHttpServletResponse response = context.response();

        createNodeServlet.doPost(request, response);
        JSONArray output = new JSONArray(context.response().getOutputAsString());

        assertEquals("Simple Post Test", output);
    }
}

however this is not working I am getting null pointer on this line

 Node node = resource.adaptTo(Node.class);

can some one help what I am missing and some tips will be of great help as I am new to AEM, and there is not much resources about unit testing sling servlets ?

1

There are 1 answers

0
mkovacek On

I think you need to register JCR_MOCK as resource resolver type

new AemContext(ResourceResolverType.JCR_MOCK);