AEM JMockit for servlet how to set node value and other parameters | Community
Skip to main content
srinivas_chann1
Level 7
June 11, 2020
Solved

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

  • June 11, 2020
  • 3 replies
  • 3301 views

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 {

@2103137
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);

}

@2785667
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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

@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-examples

3 replies

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
June 11, 2020

@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-examples

srinivas_chann1
Level 7
June 12, 2020
 
 

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);
}
srinivas_chann1
Level 7
June 12, 2020

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);
}

 

srinivas_chann1
Level 7
June 12, 2020

 

 

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);
}