This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
I am learning to write tests . can you please review my code
Post Body
the service class .
Also if you would like to share any resource or guides to learn testing . It would be a great help!
Thank you so much
have a nice day
happy coding :)
public class AuthorService {
private final AuthorRepository authorRepository;
private final AuthorMapper authorMapper;
public AuthorService(AuthorRepository authorRepository,AuthorMapper authorMapper) {
this.authorMapper=authorMapper;
this.authorRepository = authorRepository;
}
public List<AuthorResponseDTO> findAll() {
List<Author> authors = authorRepository.findAll();
return authors.stream().map(authorMapper::authortoAuthorResponseDTO).collect(Collectors.toList());
}
public AuthorResponseDTO findById(int id) {
Author author = authorRepository.findById(id).orElseThrow(() -> new AuthorNotFoundException("Author with authorId : " id " does not exists"));
return authorMapper.authortoAuthorResponseDTO(author);
}
public void deleteById(int id) {
authorRepository.findById(id).orElseThrow(() -> new AuthorNotFoundException("Author with authorId : " id " does not exists"));
authorRepository.deleteById(id);
}
public AuthorResponseDTO addNewAuthor(AuthorRequestDTO request) {
Author authorEntity = authorMapper.authorRequestDTOToAuthor(request);
// setting authorId to 0 because new entry is being created
authorEntity.setAuthorId(0);
Author savedEntity = authorRepository.save(authorEntity);
return authorMapper.authortoAuthorResponseDTO(savedEntity);
}
public AuthorResponseDTO updateAuthor(AuthorRequestDTO author,int authorId) {
authorRepository.findById(authorId).orElseThrow(()-> new AuthorNotFoundException("Author with authorId : " authorId " does not exists"));
Author authorEntity = authorMapper.authorRequestDTOToAuthor(author);
authorEntity.setAuthorId(authorId);
Author savedEntity = authorRepository.save(authorEntity);
return authorMapper.authortoAuthorResponseDTO(savedEntity);
}
the test class
@ExtendWith(MockitoExtension.class)
public class AuthorServiceTest {
@Mock
private AuthorRepository authorRepository;
@Mock
private AuthorMapper authorMapper;
@InjectMocks
private AuthorService authorService;
@Test
public void testFindAll() {
List<Author> authors = Arrays.asList(new Author() , new Author());
when(authorRepository.findAll()).thenReturn(authors);
when(authorMapper.authortoAuthorResponseDTO(any(Author.class))).thenReturn(new AuthorResponseDTO(1,"name"));
List<AuthorResponseDTO> result = authorService.findAll();
assertEquals(2,result.size());
verify(authorRepository,times(1)).findAll();
}
@Test
public void findByIdSuccess() {
Author author = new Author();
when(authorRepository.findById(1)).thenReturn(Optional.of(author));
when(authorMapper.authortoAuthorResponseDTO(author)).thenReturn(new AuthorResponseDTO(1,"name"));
AuthorResponseDTO result = authorService.findById(1);
assertNotNull(result);
verify(authorRepository,times(1)).findById(1);
}
@Test
public void findByIdNotFound() {
when(authorRepository.findById(1)).thenReturn(Optional.empty());
assertThrows(AuthorNotFoundException.class, () -> authorService.findById(1));
verify(authorRepository,times(1)).findById(1);
}
@Test
public void deleteByIdSuccess() {
when(authorRepository.findById(1)).thenReturn(Optional.of(new Author()));
authorService.deleteById(1);
verify(authorRepository,times(1)).findById(1);
verify(authorRepository,times(1)).deleteById(1);
}
@Test
public void deleteByIdFailure() {
when(authorRepository.findById(1)).thenReturn(Optional.empty());
assertThrows(AuthorNotFoundException.class,() -> authorService.deleteById(1));
verify(authorRepository,times(1)).findById(1);
verify(authorRepository,never()).deleteById(anyInt());
}
@Test
public void addNewAuthorTest() {
AuthorRequestDTO requestDTO = new AuthorRequestDTO("name");
Author author = new Author();
Author savedAuthor = new Author();
when(authorMapper.authorRequestDTOToAuthor(requestDTO)).thenReturn(author);
when(authorRepository.save(author)).thenReturn(savedAuthor);
when(authorMapper.authortoAuthorResponseDTO(savedAuthor)).thenReturn(new AuthorResponseDTO(1,"name"));
AuthorResponseDTO result = authorService.addNewAuthor(requestDTO);
assertNotNull(result);
verify(authorRepository,times(1)).save(author);
}
@Test
public void updateAuthorSuccess() {
AuthorRequestDTO requestDTO = new AuthorRequestDTO("name");
Author authorEntity = new Author();
when(authorRepository.findById(1)).thenReturn(Optional.of(new Author()));
when(authorMapper.authorRequestDTOToAuthor(requestDTO)).thenReturn(authorEntity);
authorEntity.setAuthorId(1);
when(authorRepository.save(authorEntity)).thenReturn(authorEntity);
when(authorMapper.authortoAuthorResponseDTO(authorEntity)).thenReturn(new AuthorResponseDTO(1,"name"));
AuthorResponseDTO result = authorService.updateAuthor(requestDTO,1);
assertNotNull(result);
verify(authorRepository,times(1)).findById(1);
verify(authorRepository,times(1)).save(authorEntity);
}
@Test
public void updateAuthorNotFound() {
when(authorRepository.findById(1)).thenReturn(Optional.empty());
assertThrows(AuthorNotFoundException.class, () -> authorService.updateAuthor(new AuthorRequestDTO("name"),1));
verify(authorRepository,times(1)).findById(1);
verify(authorRepository,never()).save(any(Author.class));
}
Author
Account Strength
70%
Account Age
2 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,333
Link Karma
804
Comment Karma
529
Profile updated: 4 hours ago
Posts updated: 3 weeks ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 5 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/javahelp/co...