Expand my Community achievements bar.

SOLVED

AEM JMockit for servlet how to set node value and other parameters

Avatar

Level 8

Hi ,

 

i am using jmockit for creating a test case

I am trying to mock a servlet and

1>I noticing that even though i have mocked Node class it is passing null value so how to set up node class with a particular path 

In the code below i am using

@Mocked
private Node ne;

 

2>In servlet I have to ad a test case for finding value of a particular tag values in a set ,how to do that as servlet does not return vertical tag at the end of servlet it just return response and the method i am looking is a private method in the servlet . So just for this test case should  I need to change the method to public is the right way

 

@RunWith(JMockit.class)
public class MyServletTest {

@tested
private MyServlet servlet;

@Injectable
SlingHttpServletRequest request;

@Injectable
SlingHttpServletResponse response;

private Set<String> verticalTagsSet;

@Mocked
private Node ne;

 

@Before
public void init() throws Exception {
servlet = new MyServlet();

//initMocks(null);

}

@test
public void testVerticalTagPath() throws Exception {

new Expectations() {
{
// ne.getPath();
// result= "/content/abc";
}};

 

verticalTagsSet=new HashSet<String>();

verticalTagsSet.add("content-tag:aa/bb/cc");
verticalTagsSet.add("content-tag:aa/bb/cc/dd");

 

Set<String> test= new HashSet<String>();

test.addAll(verticalTagsSet);


//servlet.doGet(request, response);

servlet.mapVerticalTags(ne,verticalTagsSet);
Assert.assertEquals(test,servlet.getVerticalTagsSet());

new Verifications() {
{

}};


}

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@srinivas_chann1 ,

You can try using technologies like MockitoJUnitRunner, and wcm.io Mocks to fulfil your requirements.

 

@RunWith(MockitoJUnitRunner.class)
public class SlingServletResourceTypesExampleDS14ServletTest {

    @Rule
    public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

    @Mock
    private MockSlingHttpServletRequest req;

    @Mock
    private MockSlingHttpServletResponse res;

    @InjectMocks
    private SlingServletResourceTypesExampleDS14Servlet underTest;

    @Before
    public void setup() {
        underTest = new SlingServletResourceTypesExampleDS14Servlet();
        req = context.request();
        res = context.response();
    }
...

 

The entire example and setup can be found here https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-e...

View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

@srinivas_chann1 ,

You can try using technologies like MockitoJUnitRunner, and wcm.io Mocks to fulfil your requirements.

 

@RunWith(MockitoJUnitRunner.class)
public class SlingServletResourceTypesExampleDS14ServletTest {

    @Rule
    public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

    @Mock
    private MockSlingHttpServletRequest req;

    @Mock
    private MockSlingHttpServletResponse res;

    @InjectMocks
    private SlingServletResourceTypesExampleDS14Servlet underTest;

    @Before
    public void setup() {
        underTest = new SlingServletResourceTypesExampleDS14Servlet();
        req = context.request();
        res = context.response();
    }
...

 

The entire example and setup can be found here https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-e...

Avatar

Level 8
 
 

Thanks for the input.The link was very useful

 

When i execute the test case it throwing null pointer exception for session.How to resolve it

 

finally {
// System.out.println("session="+session);
if (null != session && session.isLive()) {
session.logout();
}
if (null != resourceResolver || resourceResolver.isLive()) {
resourceResolver.close();
}
}

 

I tried by doing below but did not solve

 

private Session session;


@Before
public void init() throws Exception {
servletUnderTest = new servletUnderTest ();
req = context.request();
res = context.response();

session = context.resourceResolver().adaptTo(Session.class);
}

Avatar

Community Advisor

You need to mock the AEMContext session.

@Mock    
private MockResourceResolver resolver;

or

@Mock
private ResourceResolver resolver = context.resourceResolver();

So your session is passed in from the AEMContextMock
from example: https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples

Avatar

Level 8

Thanks again for the inputs the issue is the below code in servlet is giving null exception  for resourceResolverFactory  .how to mock that using aemContext

 

Servelt class

public void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response){
....
...
Map<String, Object> subService = new HashMap<>();
subService.put(ResourceResolverFactory.SUBSERVICE, SUB_SERVICE);
resourceResolver = resourceResolverFactory.getServiceResourceResolver(subService);
session = resourceResolver.adaptTo(Session.class);
......
}

 

Avatar

Level 8

Thanks for the input.The link was very useful

 

When i execute the test case it throwing null pointer exception for session  and resolver.How to resolve it

 

finally {
// System.out.println("session="+session);
if (null != session && session.isLive()) {
session.logout();
}
if (null != resourceResolver || resourceResolver.isLive()) {
resourceResolver.close();
}
}

 

I tried by doing below but did not solve

 

private Session session;


@Before
public void init() throws Exception {
servletUnderTest = new servletUnderTest ();
req = context.request();
res = context.response();

session = context.resourceResolver().adaptTo(Session.class);
}

 

Avatar

Level 8

 

 

Thanks again for the inputs the issue is the below code in servlet is giving null exception  for resourceResolverFactory  .how to mock that using aemContext

 

Map<String, Object> subService = new HashMap<>();
subService.put(ResourceResolverFactory.SUBSERVICE, SUB_SERVICE);
resourceResolver = resourceResolverFactory.getServiceResourceResolver(subService);
session = resourceResolver.adaptTo(Session.class);

}

 

I tried by doing below but did not solve

 

private Session session;


@Before
public void init() throws Exception {
servletUnderTest = new servletUnderTest ();
req = context.request();
res = context.response();

session = context.resourceResolver().adaptTo(Session.class);
}