Merge pull request 'Completed base tests for comments' (#2) from testing-base into master
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
95
src/test/java/org/zaine/app/service/CommentsServiceTest.java
Normal file
95
src/test/java/org/zaine/app/service/CommentsServiceTest.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package org.zaine.app.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.zaine.app.model.Comments;
|
||||
import org.zaine.app.repositories.CommentsRepository;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class CommentsServiceTest {
|
||||
@InjectMocks
|
||||
private CommentsService commentsService;
|
||||
|
||||
@Mock
|
||||
private CommentsRepository commentsRepository;
|
||||
|
||||
@Test
|
||||
public void testGetAllCommentsReturnsAllComments() {
|
||||
|
||||
Comments comment1 = new Comments();
|
||||
comment1.setContent("Comment 1");
|
||||
Comments comment2 = new Comments();
|
||||
comment2.setContent("Comment 2");
|
||||
when(commentsRepository.findAll()).thenReturn(Arrays.asList(comment1, comment2));
|
||||
|
||||
List<Comments> result = commentsService.getAllComments();
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("Comment 1", result.get(0).getContent());
|
||||
assertEquals("Comment 2", result.get(1).getContent());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllCommentsBySlugReturnsFilteredComments() {
|
||||
String slug = "page-1";
|
||||
Comments comment1 = new Comments();
|
||||
comment1.setContent("Comment 1");
|
||||
comment1.setPageSlug("page-1");
|
||||
Comments comment2 = new Comments();
|
||||
comment2.setContent("Comment 2");
|
||||
comment2.setPageSlug("page-2");
|
||||
when(commentsRepository.findAll()).thenReturn(Arrays.asList(comment1, comment2));
|
||||
|
||||
List<Comments> result = commentsService.getAllCommentsBySlug(slug);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Comment 1", result.get(0).getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommentByIdReturnsComment() {
|
||||
Comments comment = new Comments();
|
||||
Integer id = comment.getId();
|
||||
when(commentsRepository.findById(id)).thenReturn(java.util.Optional.of(comment));
|
||||
|
||||
Comments result = commentsService.getCommentById(id);
|
||||
assertSame(comment, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCommentSavesComment() {
|
||||
Comments comment = new Comments();
|
||||
comment.setContent("New Comment");
|
||||
when(commentsRepository.save(comment)).thenReturn(comment);
|
||||
commentsService.addComment(comment);
|
||||
|
||||
verify(commentsRepository).save(comment);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommentThreadReturnsThread() {
|
||||
Integer commentId = 1;
|
||||
Comments comment1 = new Comments();
|
||||
comment1.setContent("Reply 1");
|
||||
Comments comment2 = new Comments();
|
||||
comment2.setContent("Reply 2");
|
||||
when(commentsRepository.findCommentThread(commentId)).thenReturn(Arrays.asList(comment1, comment2));
|
||||
|
||||
List<Comments> result = commentsService.getCommentThread(commentId);
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("Reply 1", result.get(0).getContent());
|
||||
assertEquals("Reply 2", result.get(1).getContent());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,9 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.zaine.app.model.Notes;
|
||||
import org.zaine.app.repositories.NotesRepository;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class NotesServiceTest {
|
||||
|
||||
@InjectMocks
|
||||
|
||||
Reference in New Issue
Block a user