-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Comment,Issue에 대한 Repository,Service JUnit Test코드 추가
- Loading branch information
Showing
8 changed files
with
691 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/java/se/issuetrackingsystem/CommentRepositoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package se.issuetrackingsystem; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import se.issuetrackingsystem.comment.domain.Comment; | ||
import se.issuetrackingsystem.comment.repository.CommentRepository; | ||
import se.issuetrackingsystem.issue.domain.Issue; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@DataJpaTest | ||
public class CommentRepositoryTests { | ||
@Autowired | ||
CommentRepository commentRepository; | ||
|
||
@Test | ||
@DisplayName("코멘트 만들기") | ||
void createIssue(){ | ||
//given | ||
Comment comment1 = new Comment(); | ||
comment1.setMessage("hello1"); | ||
|
||
Comment comment2 = new Comment(); | ||
comment2.setMessage("hello2"); | ||
|
||
//when | ||
Comment result1 = this.commentRepository.save(comment1); | ||
Comment result2 = this.commentRepository.save(comment2); | ||
|
||
//then | ||
assertEquals(comment1,result1); | ||
assertEquals(comment2,result2); | ||
} | ||
|
||
@Test | ||
@DisplayName("코멘트 찾기") | ||
void getIssue(){ | ||
//given | ||
Comment comment1 = new Comment(); | ||
comment1.setMessage("hello1"); | ||
this.commentRepository.save(comment1); | ||
|
||
Comment comment2 = new Comment(); | ||
comment2.setMessage("hello2"); | ||
this.commentRepository.save(comment2); | ||
|
||
//when | ||
Comment result1 = this.commentRepository.findById(comment1.getId()).get(); | ||
Comment result2 = this.commentRepository.findById(comment2.getId()).get(); | ||
List<Comment> results = this.commentRepository.findAll(); | ||
|
||
//then | ||
assertEquals(comment1,result1); | ||
assertEquals(comment2,result2); | ||
assertEquals(results.size(),2); | ||
} | ||
|
||
@Test | ||
@DisplayName("이슈 삭제") | ||
void deleteIssue() { | ||
//given | ||
Comment comment1 = new Comment(); | ||
comment1.setMessage("hello1"); | ||
this.commentRepository.save(comment1); | ||
|
||
//when | ||
this.commentRepository.delete(comment1); | ||
|
||
//then | ||
assertTrue(this.commentRepository.findById(comment1.getId()).isEmpty()); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/test/java/se/issuetrackingsystem/CommentServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package se.issuetrackingsystem; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import se.issuetrackingsystem.comment.domain.Comment; | ||
import se.issuetrackingsystem.comment.repository.CommentRepository; | ||
import se.issuetrackingsystem.comment.service.CommentService; | ||
import se.issuetrackingsystem.issue.domain.Issue; | ||
import se.issuetrackingsystem.issue.repository.IssueRepository; | ||
import se.issuetrackingsystem.user.domain.User; | ||
import se.issuetrackingsystem.user.repository.UserRepository; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
public class CommentServiceTests { | ||
|
||
CommentService commentService; | ||
@MockBean | ||
CommentRepository commentRepository; | ||
@MockBean | ||
IssueRepository issueRepository; | ||
@MockBean | ||
UserRepository userRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
commentService = new CommentService(commentRepository, issueRepository, userRepository); | ||
} | ||
|
||
@Test | ||
@DisplayName("코멘트 생성") | ||
void commentCreate() { | ||
//given | ||
Comment comment = new Comment(); | ||
Issue issue = mock(Issue.class); | ||
User user = mock(User.class); | ||
comment.setMessage("Hello"); | ||
comment.setIssue(issue); | ||
comment.setAuthor(user); | ||
|
||
when(issueRepository.findById(1L)).thenReturn(Optional.of(issue)); | ||
when(userRepository.findById(1L)).thenReturn(Optional.of(user)); | ||
when(commentRepository.save(comment)).thenReturn(comment); | ||
|
||
//when | ||
Comment result = commentService.create(1L, "Hello", 1L); | ||
|
||
//then | ||
assertEquals("Hello", result.getMessage()); | ||
assertEquals(issue, result.getIssue()); | ||
assertEquals(user, result.getAuthor()); | ||
} | ||
|
||
@Test | ||
@DisplayName("코멘트 수정") | ||
void commentModify() { | ||
//given | ||
Comment comment = new Comment(); | ||
comment.setMessage("Hello"); | ||
|
||
when(commentRepository.findById(comment.getId())).thenReturn(Optional.of(comment)); | ||
when(commentRepository.save(comment)).thenReturn(comment); | ||
|
||
//when | ||
Comment result = commentService.modify(comment.getId(), "Hi"); | ||
|
||
//then | ||
assertEquals("Hi", result.getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("코멘트 리스트 찾기") | ||
void commentGetList() { | ||
//given | ||
Issue issue = mock(Issue.class); | ||
|
||
Comment comment1 = new Comment(); | ||
comment1.setIssue(issue); | ||
comment1.setId(1L); | ||
comment1.setMessage("1M"); | ||
|
||
Comment comment2 = new Comment(); | ||
comment2.setMessage("2M"); | ||
comment2.setIssue(issue); | ||
comment2.setId(2L); | ||
|
||
List<Comment> comments = Arrays.asList( | ||
comment1,comment2 | ||
); | ||
|
||
when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); | ||
when(commentRepository.findAllByIssue(issue)).thenReturn(comments); | ||
|
||
//when | ||
List<Comment> result = commentService.getList(issue.getId()); | ||
|
||
//then | ||
assertEquals(result.size(),2); | ||
assertEquals("1M",result.get(0).getMessage()); | ||
assertEquals("2M",result.get(1).getMessage()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/se/issuetrackingsystem/IssueRepositoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package se.issuetrackingsystem; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import se.issuetrackingsystem.issue.domain.Issue; | ||
import se.issuetrackingsystem.issue.repository.IssueRepository; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@DataJpaTest | ||
public class IssueRepositoryTests { | ||
@Autowired | ||
IssueRepository issueRepository; | ||
|
||
@Test | ||
@DisplayName("이슈 만들기") | ||
void createIssue(){ | ||
//given | ||
Issue issue1 = new Issue(); | ||
issue1.setTitle("hello1"); | ||
issue1.setDescription("hi1"); | ||
|
||
Issue issue2 = new Issue(); | ||
issue2.setTitle("hello2"); | ||
issue2.setDescription("hi2"); | ||
|
||
//when | ||
Issue result1 = this.issueRepository.save(issue1); | ||
Issue result2 = this.issueRepository.save(issue2); | ||
|
||
//then | ||
assertEquals(issue1,result1); | ||
assertEquals(issue2,result2); | ||
} | ||
|
||
@Test | ||
@DisplayName("이슈 찾기") | ||
void getIssue(){ | ||
//given | ||
Issue issue1 = new Issue(); | ||
issue1.setTitle("hello1"); | ||
issue1.setDescription("hi1"); | ||
this.issueRepository.save(issue1); | ||
|
||
Issue issue2 = new Issue(); | ||
issue2.setTitle("hello2"); | ||
issue2.setDescription("hi2"); | ||
this.issueRepository.save(issue2); | ||
|
||
//when | ||
Issue result1 = this.issueRepository.findById(issue1.getId()).get(); | ||
Issue result2 = this.issueRepository.findById(issue2.getId()).get(); | ||
List<Issue> results = this.issueRepository.findAll(); | ||
|
||
//then | ||
assertEquals(issue1,result1); | ||
assertEquals(issue2,result2); | ||
assertEquals(results.size(),2); | ||
} | ||
|
||
@Test | ||
@DisplayName("이슈 삭제") | ||
void deleteIssue() { | ||
//given | ||
Issue issue1 = new Issue(); | ||
issue1.setTitle("hello1"); | ||
issue1.setDescription("hi1"); | ||
this.issueRepository.save(issue1); | ||
|
||
//when | ||
this.issueRepository.delete(issue1); | ||
|
||
//then | ||
assertTrue(this.issueRepository.findById(issue1.getId()).isEmpty()); | ||
} | ||
} |
Oops, something went wrong.