Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to mock super class that has injected dependencies

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.

like image 702
ATEF CHAREF Avatar asked Feb 15 '17 17:02

ATEF CHAREF


People also ask

How do you mock injected objects?

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.

Can we mock super class method?

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".

Is mocking a dependency injection?

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.

What is difference between @mock and @injectmock?

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


1 Answers

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.

like image 159
Pramendra Raghuwanshi Avatar answered Nov 12 '22 03:11

Pramendra Raghuwanshi