简体   繁体   中英

Nullpointer when using Mockito for unit tests

I am creating a spring boot API for my application. I am attempting to unit test my service implementation using mockito to mock out the detail. The service will add a new building entity to the database. Below is the Service implementation and the test implementation. Building Service:

@Slf4j
@Service
public class BuildingServiceImpl implements BuildingService {

private BuildingRepository buildingRepository;
private BuildingRequestToEntityMapper buildingRequestToEntityMapper;

public BuildingServiceImpl(BuildingRepository buildingRepository, BuildingRequestToEntityMapper 
buildingRequestToEntityMapper){
this.buildingRepository=buildingRepository;
this.buildingRequestToEntityMapper=buildingRequestToEntityMapper;
}

public HttpStatus addBuilding(BuildingRequest buildingRequest){
    log.info("Inside addBuilding() service");
    BuildingEntity buildingEntity = buildingRequestToEntityMapper.map(buildingRequest);
    buildingRepository.save(buildingEntity);
    log.info("Building saved "+ buildingEntity);
    return HttpStatus.CREATED;
}

BuildingServiceImpl_UT

@RunWith(MockitoJUnitRunner.class)
public class BuildingServiceImpl_UT {

@Mock
BuildingRequestToEntityMapper buildingRequestToEntityMapper;

@Mock
BuildingRepository buildingRepository;

@InjectMocks
BuildingServiceImpl buildingServiceImpl;

@Test
public void buildingService_MapsRequest_AndSaveEntity(){
BuildingRequest buildingRequest = BuildingRequest.builder()
            .name("TestName")
            .telephone("4444444444")
            .postcode("TEst")
            .address("testAddress").build();

   when(buildingServiceImpl.addBuilding(any(BuildingRequest.class))).thenReturn(HttpStatus.CREATED);
    when(buildingRepository.save(any(BuildingEntity.class))).thenReturn(new BuildingEntity());

    buildingServiceImpl.addBuilding(buildingRequest);
    verify(buildingRepository, times(1)).save(any());
}

I have mocked the mapper and repository and injected them into the service, but when i run the test I get a null pointer exception at the first when().thenReturn() statement in the test class. Any help please. Thanks

I don't understand your first when().thenReturn()! You try to do this on the buildingServiceImpl wich is not a mock! Further more this makes no sense because you want to test this methode! I think you should define a when().thenReturn() for the mock buildingRequestToEntityMapper , but in your implementation you don't need to define a return for buildingRequestToEntityMapper.map() . In this case the variable buildingEntity will have the value null which should work in your test case.

@RunWith(MockitoJUnitRunner.class)
public class BuildingServiceImpl_UT {

@Mock
BuildingRequestToEntityMapper buildingRequestToEntityMapper;

@Mock
BuildingRepository buildingRepository;

@InjectMocks
BuildingServiceImpl buildingServiceImpl;

    @Test
    public void buildingService_MapsRequest_AndSaveEntity(){
    BuildingRequest buildingRequest = BuildingRequest.builder()
                .name("TestName")
                .telephone("4444444444")
                .postcode("TEst")
                .address("testAddress").build();


        when(buildingRepository.save(any(BuildingEntity.class))).thenReturn(new BuildingEntity());

        buildingServiceImpl.addBuilding(buildingRequest);
        verify(buildingRepository, times(1)).save(any());
        verify(buildingRequestToEntityMapper).map(any());
    }

This:

when(buildingServiceImpl.addBuilding(any(BuildingRequest.class))).thenReturn(HttpStatus.CREATED);

is not necessary, you want to test method: addBuilding not mock it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM