Jech
Jech

Reputation: 349

Junit mocking for hashmap having value as multivaluemap

I have a problem, How to set value for this hashmap and mock it

Map <String, MultiValueMap<String, Integer>> idsrcMp = new HashMap<>(3);

Upvotes: 3

Views: 14465

Answers (1)

alexbt
alexbt

Reputation: 17065

I'm not sure what you mean by "set value and mock it". You want to set values or mock it ?

You can use the Map / MultiValueMap this way:

Map<String, MultiValueMap<String, Integer>> idsrcMp = new HashMap<>(3);
MultiValueMap<String, Integer> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.put("item", Collections.emptyList()); // insert a List here
idsrcMp.put("first", multiValueMap);

Using Mocks:

Map map = Mockito.mock(Map.class);
MultiValueMap multiValueMap = Mockito.mock(MultiValueMap.class);

Mockito.when(map.get("mapItem")).thenReturn(multiValueMap);
Mockito.when(multiValueMap.get("multimapItem"))
       .thenReturn(Collections.emptyList());

Upvotes: 4

Related Questions