0

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

1
  • Remove when(buildingServiceImpl.addBuilding(any(BuildingRequest.class))).thenReturn(HttpStatus.CREATED); line from test. Commented Jan 2, 2020 at 4:13

2 Answers 2

1

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());
    }
Sign up to request clarification or add additional context in comments.

Comments

0

This:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.