PositionServiceImpl extends AbstractLogicallyDeletableService<PositionDTO, PositionEntity> implements PositionService { @Transactional(readOnly = true) @Override public PositionDTO getPositionDtoByName(String positionName) { Assert.notNull(positionName); PositionEntity positionEntity = positionRepository.findByName(positionName); if (positionEntity != null) { return converterService.convert(positionEntity, PositionDTO.class); } return null; } @Autowired  PositionServiceImpl(PositionRepository positionRepository,
                             ConverterService converterService); @Transactional(readOnly = true) @Override List<PositionDTO> getPositionsById(List<Long> ids); @Transactional(readOnly = true) @Override PositionDTO getPositionDtoById(Long positionId); @Transactional(readOnly = true) @Override PositionDTO getPositionDtoByName(String positionName); @Transactional @Override Long saveOrUpdate(PositionDTO position);  }
@Test(expected = IllegalArgumentException.class) public void getPositionDtoByNameShouldThrowIllegalArgumentExceptionWhenPositionNameIsNull() { positionService.getPositionDtoByName(null); }
@Test public void getPositionDtoByNameShouldReturnNullPositionDtoWhenPositionNameIsNonExistent() { given(positionRepository.findByName(POSITION_NAME_NON_EXISTENT)).willReturn(null); PositionDTO positionDTO = positionService.getPositionDtoByName(POSITION_NAME_NON_EXISTENT); assertThat(positionDTO, nullValue()); then(positionRepository).should().findByName(POSITION_NAME_NON_EXISTENT); verifyZeroInteractions(converterService); }
@Test public void getPositionDtoByNameShouldReturnExistingPositionDtoWhenPositionNameIsExisting() { given(positionRepository.findByName(DEVELOPER_NAME)).willReturn(developerEntity); given(converterService.convert(developerEntity, PositionDTO.class)).willReturn(developerDto); PositionDTO positionDTO = positionService.getPositionDtoByName(DEVELOPER_NAME); assertThat(positionDTO, notNullValue()); assertThat(positionDTO, equalTo(developerDto)); then(positionRepository).should().findByName(DEVELOPER_NAME); then(converterService).should().convert(developerEntity, PositionDTO.class); }