I am trying to write a test case in JUNIT5.
I want to do unit test on the below class
Class Service{
@Autowired
ObjectHolder objectHolder;
UtilityServiceDTOMapper<T> serviceDTOMapper = objectHolder.getImplementation();
List<Records> recordList = serviceDTOMapper.mapper("Test");//Line I want to do unit test
}
Junit
@Test
void test(){
ReflectionTestUtils.setField(ObjectHolder, "implementations", mock(DTOMapper.class)));
}
//---Now i want to do the unit test for the above line i mentioned.
// I tried like
when(serviceDTOMapper.mapper(any())).thenReturn(someListOfValues);
//This is not working , getting an empty list not 'someListOfValues'
DTOMapper.classis of typeUtilityServiceDTOMapperinterface.- there is a method
mapperinsideserviceDTOMapper. - Now I want to do a mocking on this object
serviceDTOMapperlike below
when(serviceDTOMapper.mapper(any())).thenReturn(someListOfValues);
the above code is what i tried, but its not working.
It`s pretty simple to do. You need to call the method which uses your mapper and only then you could mock a behavior of your mock. Test should look like this: