StateServiceImpl implements StateService { @Transactional(readOnly = true) @Override public StateDTO getStateDtoByName(String name) { Assert.notNull(name); return converterService.convert(statesRepository.findByName(name), StateDTO.class); }  @Transactional(readOnly = true) @Override StateDTO getStateDtoById(Long id); @Transactional(readOnly = true) @Override StateDTO getStateDtoByName(String name);  }
@Test public void getStateDtoByNameShouldReturnStateDtoWhenValidNameIsGiven() { given(statesRepository.findByName(VALID_STATE_NAME)).willReturn(validStatesEntity); given(converterService.convert(validStatesEntity, StateDTO.class)).willReturn(validStateDTO); StateDTO expectedStateDTO = StateDTO.builder().id(VALID_STATE_ID).name(VALID_STATE_NAME).build(); StateDTO result = stateService.getStateDtoByName(VALID_STATE_NAME); assertThat(result, notNullValue()); assertThat(result, is(expectedStateDTO)); }
@Test public void getStateDtoByNameShouldNotReturnStateDtoWhenInvalidNameIsGiven() { given(statesRepository.findByName(INVALID_STATE_NAME)).willReturn(null); StateDTO result = stateService.getStateDtoByName(INVALID_STATE_NAME); assertThat(result, nullValue()); }
@Test(expected = IllegalArgumentException.class) public void getStateDtoByNameShouldThrowIllegalArgumentExceptionWhenNameIsNull() { StateDTO result = stateService.getStateDtoByName(null); }