I have a project that uses Spring MVC. I'm trying to write unit tests for the service module, which is in the architecture of the project. All service classes extend from super class named "BaseService". BaseService is like this:
public abstract class BaseService {
private static final Logger logger = LoggerFactory.getLogger(BaseService.class);
@Autowired(required = true)
private HttpServletRequest request;
@Autowired
private ReloadableResourceBundleMessageSource messageSource;
/*
* Injecting Mapper
*/
@Resource
private Mapper mapper;
...
public <T extends BaseBVO, S extends BaseVO> T voToBvo (S vo, Class<? extends BaseBVO> bvoClass) {
if (vo != null)
{
return (T) mapper.map(vo , bvoClass);
}
else
{
return null;
}
}
now I have a method in the service module that use the method:
"voToBvo (S vo, Class<? extends BaseBVO> bvoClass")
like this:
public List<AdcOptionBVO> findOptionByAyantDroitIdAndProduitId (Long idAyantDroit, String idProduit) {
....
list = listVoToBvo(adcList , AdcOptionBVO.class);
logger.info("Returning List of ayrp count= {}" , list.size());
return list;
}
My test is like this:
@RunWith(MockitoJUnitRunner.class)
public class AdcServiceImplTest{
@Mock
private Mapper mapper;
@Test
public void shouldSuccessFindOptionByAyantDroitIdandProduitId() {
//Given
List<AdcVO> adcVOList = new ArrayList<>();
adcVOList.add(new AdcVO());
List<AdcOptionBVO> listAdcOptionBVO = new ArrayList<>();
listAdcOptionBVO.add(new AdcOptionBVO());
List<BaseBVO> baseBVOs = new ArrayList<>();
//When
when(repository
.finAdcByOptionOrderByRUAndPrio(anyLong(), anyString())).thenReturn(adcVOList);
when(baseService.listVoToBvo(adcVOList, AdcOptionBVO.class)).thenReturn(baseBVOs);
//Then
assertEquals(adcService
.findOptionByAyantDroitIdAndProduitId(anyLong(), anyString()).size(), adcVOList.size());
}
}
I get a java.lang.NullPointerException in BaseService.java when calling the mapper.
@Resource
private Mapper mapper;
the mapper is null!
I want to mock the method:
listVoToBvo(adcList , AdcOptionBVO.class);
Please help.
Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have external dependencies in the class we want to mock. We can specify the mock objects to be injected using @Mock or @Spy annotations.
As a quick way around, I just added a different proxy method on the subclass that calls super's run. Then you can mock the "proxy".
Dependency injection is a way to scale the mocking approach. If a lot of use cases are relying on the interaction you'd like to mock, then it makes sense to invest in dependency injection. Systems that lend themselves easily to dependency injection: An authentication/authorization service.
@Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class. Annotated class to be tested dependencies with @Mock annotation.
I will suggest you, first to change in your BaseService file. Change the way of writing @Autowired as follows
private HttpServletRequest request;
@Autowired(required = true)
public void setSpellChecker( HttpServletRequest request ){
this.request = request;
}
@Autowired
private ReloadableResourceBundleMessageSource messageSource;
public void setSpellChecker( ReloadableResourceBundleMessageSource messageSource ){
this.messageSource = messageSource;
}
And when you will do that, now you can easily achieve test-cases with dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With