Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Junit Test Case with RequestParameterMap not working

Avatar

Avatar
Give Back 3
Level 2
prashanth55
Level 2

Likes

6 likes

Total Posts

18 posts

Correct Reply

2 solutions
Top badges earned
Give Back 3
Give Back
Ignite 1
Validate 1
Boost 5
View profile

Avatar
Give Back 3
Level 2
prashanth55
Level 2

Likes

6 likes

Total Posts

18 posts

Correct Reply

2 solutions
Top badges earned
Give Back 3
Give Back
Ignite 1
Validate 1
Boost 5
View profile
prashanth55
Level 2

08-03-2021

Hi All,

 

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

 

Below one is the code snippet .

 

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

View Entire Topic

Avatar

Avatar
Ignite 1
Level 4
davidjgonzalezzzz
Level 4

Likes

54 likes

Total Posts

60 posts

Correct Reply

20 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
Level 4
davidjgonzalezzzz
Level 4

Likes

54 likes

Total Posts

60 posts

Correct Reply

20 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
davidjgonzalezzzz
Level 4

09-03-2021

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