Junit Test Case with RequestParameterMap not working | Community
Skip to main content
Level 2
Mar 9, 2021 4:50 AM UTC
Solved

Junit Test Case with RequestParameterMap not working

Hi All,

 

Am writing one test case with RequestParameterMap, but it's throwing nullpointer exception . 

 

Below one is the code snippet .

 

@2785667
public void validateUserData() {
Map<String, RequestParameter[]> params = new HashMap<>();
params.put("firstName", getRequestParameter("firstName", "test"));
params.put("lastName", getRequestParameter("lastName", "test last"));

when(request.getParameterMap()).thenReturn(params);
RequestParameterMap requestParameterMap = Mockito.mock(RequestParameterMap.class);
when(requestParameterMap.entrySet()).thenReturn(params.entrySet());

UserInfo user = userService.getUserInfo(requestParameterMap);
assertEquals("firstName", user.getFirstName());
}

 

when i try to get the values from requestParameterMap , it's giving null pointer exception.

 

Could you please help me resolve  this  issue .

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
davidjgonzalezzzzAccepted solution
Employee
Mar 9, 2021 3:08 PM UTC

Have you tried using AEM Mock's AemContext to create mock sling request context?

 

I highly, highly recommend using AEM Mocks when possible rather than mocking out everything yourself. These sorts of problems should go away, and also lets you write more natural tests.

 

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

 

 

2 replies

davidjgonzalezzzzAccepted solution
Employee
Mar 9, 2021 3:08 PM UTC

Have you tried using AEM Mock's AemContext to create mock sling request context?

 

I highly, highly recommend using AEM Mocks when possible rather than mocking out everything yourself. These sorts of problems should go away, and also lets you write more natural tests.

 

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

 

 

Translate
SureshDhulipudi
New Participant
Mar 9, 2021 10:58 AM UTC

can you please post complete test class here, 

 

== The below code is correct and should work ==

RequestParameterMap requestParameterMap = mock(RequestParameterMap.class);
when(requestParameterMap.entrySet()).thenReturn(params.entrySet());

 

=== try this one

when(parameterMap.isEmpty()).thenReturn(Boolean.FALSE);
when(parameterMap.getValue("firstName")).thenReturn("test");

Translate