Reputation: 19
I have gone through couple of related questions but those are not help me out. Actually, I have a method whose return type is Map<?,?>
and I want to do unit test using Mockito. Sample Code is:
Map<?, ?> resultMap = dataServiceMapper.getData(serviceContext, requestData.getId());
and I want to Mock this:
dataServiceMapper.getData(serviceContext, requestData.getId()):
Mocking Code is :
Mockito.when(mapperMock.getData(any(ServiceContext.class), anyString())).thenReturn(value).
In value if I return normal Map with String then its throwing compile time error except null (as per documentation).
But I want to add some data like String so that after getting mocking map data I can do further work.
Upvotes: -1
Views: 507
Reputation: 30819
To mock a method, all you need to do is pass a Map
without generic type. Generics are removed after compilation anyway, and all you are doing is writing a test so, implementation similar to below example should work:
//Actual method
public Map<?, ?> getData(String input){
return null;
}
//Test
public void test() {
Test2 t2 = Mockito.mock(Test2.class);
Map result = new HashMap();
Mockito.when(t2.getData(Mockito.anyString())).thenReturn(result);
}
Upvotes: 0