Hi ,
I am using the below code in post servlet .While debugging junit code getting null in session object .Can some one please help how to pass session to junit class.
Views
Replies
Total Likes
Hi @djohn98390536 ,
Instead of registering the session object as an adapter, try a when/then to return the object
lenient().when(resourceResolver.adaptTo(Session.class)).thenReturn(session)
Regards,
Anupam
Hi @anupampat tried the above code not working getting below error
How are you initializing your aemcontext ?
private final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
Is it possible to share the complete code ?
And also can you once try using ResourceResolverType.JCR_MOCK ?
@ExtendWith(AemContextExtension.class)
class CreatesampleServletTest {
private final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
@InjectMocks
private CreatesampleServlet createsample = new CreatesampleServlet();
@Mock
private MockSlingHttpServletRequest request;
@Mock
private MockSlingHttpServletResponse response;
@Mock
private ResourceResolver resourceResolver;
@Mock
private Session session;
@BeforeEach
void setUp() throws Exception {
request = context.request();
response = context.response();
resourceResolver = context.request().getResourceResolver();
context.registerAdapter(ResourceResolver.class, Session.class, session);
context.request().setQueryString("path=/content/dam/demo/sampleimage&title=sample");
}
@test
void test_post() throws ServletException, IOException {
createsample.doPost(request,response);
assertEquals("sample",response.getOutputAsString());
}
Above code working for me
can u share how it is working for you . i am still getting error.
SERVLET TEST class
package projectpackage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
@ExtendWith(AemContextExtension.class)
class DemoServletTest {
private final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
@InjectMocks
private DemoServlet demo = new DemoServlet();
@Mock
private MockSlingHttpServletRequest request;
@Mock
private MockSlingHttpServletResponse response;
@Mock
private ResourceResolver resourceResolver;
@Mock
private Session session;
@BeforeEach
void setUp() throws Exception {
request = context.request();
response = context.response();
resourceResolver = context.request().getResourceResolver();
context.registerAdapter(ResourceResolver.class, Session.class, session);
context.request().setQueryString("path=/content/dam/demo/sampleimage&title=sample");
}
@Test
void test_post() throws ServletException, IOException {
demo.doPost(request,response);
assertEquals("admin",response.getOutputAsString());
}
}
SERVLET
package projectpackage;
import java.io.IOException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
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;
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Demo Servlet", "sling.servlet.methods=" + HttpConstants.METHOD_POST, "sling.servlet.selectors=" + "selector1", "sling.servlet.resourceTypes=" + "project/components/demo", "sling.servlet.extensions=" + "json"})
public class DemoServlet extends SlingAllMethodsServlet {
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String sourcePath = request.getParameter("Path");
String title = request.getParameter("title");
ResourceResolver resolver = request.getResourceResolver();
Session session = resolver.adaptTo(Session.class);
//String assetDetails = createAssetNode(session, sourcePath,Title);
response.getWriter().write(session.getUserID());
}
}
@djohn98390536 Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes
No @kautuk_sahni it is still not working for me .Still i need more info on this.
Views
Replies
Total Likes
Are you still getting session as a null or something else.
Above code which i shared is working fine.
can you share error if something else
@djohn98390536 Also I can see String sourcePath = request.getParameter("Path");
However in junit you are setting lowercase p
context.request().setQueryString("path=/content/dam/demo/sampleimage&title=sample");
Please correct it, else sourcePath will be null
package com.proeject.core.servlets;//change this package path according to your proeject structure
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
@ExtendWith(AemContextExtension.class)
class DemoServletTest {
private final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
@InjectMocks
private DemoServlet demo = new DemoServlet();
@Mock
private MockSlingHttpServletRequest request;
@Mock
private MockSlingHttpServletResponse response;
@Mock
private ResourceResolver resourceResolver;
@Mock
private Session session;
@BeforeEach
void setUp() throws Exception {
request = context.request();
response = context.response();
resourceResolver = context.request().getResourceResolver();
context.registerAdapter(ResourceResolver.class, Session.class, session);
context.request().setQueryString("path=/content/dam/demo/sampleimage&title=sample");
}
@Test
void test_post() throws ServletException, IOException {
demo.doPost(request,response);
assertEquals("admin/content/dam/demo/sampleimage",response.getOutputAsString());
}
}
package com.project.core.servlets;//change this as per your project structure
import java.io.IOException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
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;
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Demo Servlet", "sling.servlet.methods=" + HttpConstants.METHOD_POST, "sling.servlet.selectors=" + "selector1", "sling.servlet.resourceTypes=" + "project/components/demo", "sling.servlet.extensions=" + "json"})
public class DemoServlet extends SlingAllMethodsServlet {
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String sourcePath = request.getParameter("path");
String title = request.getParameter("title");
ResourceResolver resolver = request.getResourceResolver();
Session session = resolver.adaptTo(Session.class);
//String assetDetails = createAssetNode(session, sourcePath,Title);
response.getWriter().write(session.getUserID()+sourcePath);
}
}
Thanks
Views
Replies
Total Likes
yes in my case i m using the session in a method there i am still getting session as null
String assetDetails = createAssetNode(session, sourcePath,Title);
Views
Replies
Total Likes
Could you please confirm if yours Servlet is registered by path or resourceType?
Views
Replies
Total Likes
My servlet is registered by path
Views
Replies
Total Likes
Anyway it is working for register by path as well I checked just now
Thanks
Views
Replies
Total Likes
Hi
First thing you can copy past above servlet and test class it should pass in you system
If above code pass check following points in your servlet:-
ResourceResolverType.JCR_MOCK
Thanks
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies