ChannelServiceImpl extends AbstractLogicallyDeletableService<ChannelDTO, ChannelEntity> implements ChannelService { @Transactional(readOnly = true) @Override public ChannelDTO getChannelDtoByName(String channelName) { Assert.notNull(channelName); ChannelEntity channelEntity = channelRepository.findByName(channelName); if (channelEntity != null) { return converterService.convert(channelEntity, ChannelDTO.class); } return null; } @Autowired  ChannelServiceImpl(ChannelRepository channelRepository,
                            ConverterService converterService); @Transactional(readOnly = true) @Override ChannelDTO getChannelDtoById(Long channelId); @Transactional(readOnly = true) @Override ChannelDTO getChannelDtoByName(String channelName); @Transactional @Override Long saveOrUpdate(ChannelDTO channel);  }
@Test(expected = IllegalArgumentException.class) public void getChannelDtoByNameShouldThrowIllegalArgumentExceptionWhenChannelNameIsNull() { channelService.getChannelDtoByName(null); }
@Test public void getChannelDtoByNameShouldReturnNullChannelDtoWhenChannelNameIsNonExistent() { given(channelRepository.findByName(CHANNEL_NAME_NON_EXISTENT)).willReturn(null); ChannelDTO channelDTO = channelService.getChannelDtoByName(CHANNEL_NAME_NON_EXISTENT); assertThat(channelDTO, nullValue()); then(channelRepository).should().findByName(CHANNEL_NAME_NON_EXISTENT); verifyZeroInteractions(converterService); }
@Test public void getChannelDtoByNameShouldReturnExistingChannelDtoWhenChannelNameIsExisting() { given(channelRepository.findByName(CHANNEL_NAME_FACEBOOK)).willReturn(facebookEntity); given(converterService.convert(facebookEntity, ChannelDTO.class)).willReturn(facebookDto); ChannelDTO channelDTO = channelService.getChannelDtoByName(CHANNEL_NAME_FACEBOOK); assertThat(channelDTO, notNullValue()); assertThat(channelDTO, equalTo(facebookDto)); then(channelRepository).should().findByName(CHANNEL_NAME_FACEBOOK); then(converterService).should().convert(facebookEntity, ChannelDTO.class); }