AEM MockitoJUnitRunner for servlet how to set resourceResolverFactory for AEMContext

Avatar

Avatar
Validate 25
Level 4
srinivas_chann1
Level 4

Likes

21 likes

Total Posts

222 posts

Correct reply

6 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
Level 4
srinivas_chann1
Level 4

Likes

21 likes

Total Posts

222 posts

Correct reply

6 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
srinivas_chann1
Level 4

12-06-2020

Hi,

 

Could some one provide inputs on 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){

@reference
private ResourceResolverFactory resourceResolverFactory;

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

response.setContentType(APPLICATION_JSON_UTF8);
}

 

 

In test class:-

@RunWith(MockitoJUnitRunner.class)
public class MyServletTest {

@InjectMocks
private MyServlet servlet;

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

@Mock
private MockSlingHttpServletRequest req;

@Mock
private MockSlingHttpServletResponse res;

 

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

}

@test
public void doGet_shouldReturnHeaderAsExpected() throws IOException, ServletException {
servletUnderTest.doGet(req, res);
assertEquals(res.getContentType(), APPLICATION_JSON_UTF8);
}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

484 likes

Total Posts

1,048 posts

Correct reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

484 likes

Total Posts

1,048 posts

Correct reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile
Veena_Vikram
MVP

16-06-2020

@srinivas_chann1 Can you try below instead of MockRequest

 

 

@Mock
private SlingHttpServletRequest req;

@Mock
private SlingHttpServletResponse res;

 

Thanks

Veena

 

Answers (1)

Answers (1)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

14-06-2020

Check https://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service

Try to inject service in init method 

 

// prepare sling request
MockResourceResolverFactory resourceResolverFactory = new MockResourceResolverFactory();
ResourceResolver resolver = resourceResolverFactory.getResourceResolver(null);
MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(resolver);