forked from code-squad/java-qna-atdd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QnaServiceTest.java
105 lines (87 loc) · 3.39 KB
/
QnaServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package codesquad.service;
import codesquad.CannotDeleteException;
import codesquad.UnAuthorizedException;
import codesquad.domain.Answer;
import codesquad.domain.Question;
import codesquad.domain.QuestionRepository;
import codesquad.domain.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import javax.persistence.EntityNotFoundException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class QnaServiceTest {
@Mock
private QuestionRepository questionRepo;
@InjectMocks
private QnaService qnaService;
private Question question;
private Question updateQuestion;
private User user;
private User otherUser;
@Before
public void setUp() throws Exception {
user = new User("colin", "password", "colin", "colin@codesquad.kr");
otherUser = new User("jinbro", "password", "jinbro", "jinbro@codesquad.kr");
question = new Question("test", "testing!");
question.writeBy(user);
updateQuestion = new Question("modify test", "modify");
updateQuestion.writeBy(user);
}
@Test
public void create() {
Question newQuestion = new Question("new!", "new new contents");
qnaService.create(user, newQuestion.toQuestionDto());
verify(questionRepo, times(1)).save(newQuestion);
}
@Test(expected = EntityNotFoundException.class)
public void read_not_exist() {
when(questionRepo.findById(anyLong())).thenReturn(Optional.empty());
qnaService.findById(anyLong());
}
@Test(expected = EntityNotFoundException.class)
public void read_fail_not_exist() {
qnaService.findById(10000L);
}
@Test
public void findAll() {
when(questionRepo.findByDeleted(false)).thenReturn(Collections.singletonList(question));
assertEquals(1, qnaService.findAll().size());
}
@Test
public void update() {
updateQuestion.writeBy(user);
when(questionRepo.findById(anyLong())).thenReturn(Optional.of(question));
qnaService.update(user, anyLong(), updateQuestion.toQuestionDto());
}
@Test(expected = UnAuthorizedException.class)
public void update_fail_not_math_writer() {
when(questionRepo.findById(anyLong())).thenReturn(Optional.of(question));
qnaService.update(otherUser, anyLong(), updateQuestion.toQuestionDto());
}
@Test
public void delete() throws Exception {
when(questionRepo.findById(anyLong())).thenReturn(Optional.of(question));
qnaService.deleteQuestion(user, anyLong());
}
@Test(expected = UnAuthorizedException.class)
public void delete_fail_not_owner() throws Exception {
when(questionRepo.findById(anyLong())).thenReturn(Optional.of(question));
qnaService.deleteQuestion(otherUser, anyLong());
}
@Test(expected = CannotDeleteException.class)
public void delete_fail_not_answer_owner() throws Exception {
question.addAnswer(new Answer(otherUser, "test contents"));
when(questionRepo.findById(anyLong())).thenReturn(Optional.of(question));
qnaService.deleteQuestion(user, anyLong());
}
}