-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathStudentServiceTest.java
More file actions
113 lines (92 loc) · 3.13 KB
/
StudentServiceTest.java
File metadata and controls
113 lines (92 loc) · 3.13 KB
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
106
107
108
109
110
111
112
113
package com.example.demo.student;
import com.example.demo.student.exception.BadRequestException;
import com.example.demo.student.exception.StudentNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class StudentServiceTest {
//comment
@Mock private StudentRepository studentRepository;
private StudentService underTest;
@BeforeEach
void setUp() {
underTest = new StudentService(studentRepository);
}
@Test
void canGetAllStudents() {
// when
underTest.getAllStudents();
// then
verify(studentRepository).findAll();
}
@Test
void canAddStudent() {
// given
Student student = new Student(
"Jamila",
"jamila@gmail.com",
Gender.FEMALE
);
// when
underTest.addStudent(student);
// then
ArgumentCaptor<Student> studentArgumentCaptor =
ArgumentCaptor.forClass(Student.class);
verify(studentRepository)
.save(studentArgumentCaptor.capture());
Student capturedStudent = studentArgumentCaptor.getValue();
assertThat(capturedStudent).isEqualTo(student);
}
@Test
void willThrowWhenEmailIsTaken() {
// given
Student student = new Student(
"Jamila",
"jamila@gmail.com",
Gender.FEMALE
);
given(studentRepository.selectExistsEmail(anyString()))
.willReturn(true);
// when
// then
assertThatThrownBy(() -> underTest.addStudent(student))
.isInstanceOf(BadRequestException.class)
.hasMessageContaining("Email " + student.getEmail() + " taken");
verify(studentRepository, never()).save(any());
}
@Test
void canDeleteStudent() {
// given
long id = 10;
given(studentRepository.existsById(id))
.willReturn(true);
// when
underTest.deleteStudent(id);
// then
verify(studentRepository).deleteById(id);
}
@Test
void willThrowWhenDeleteStudentNotFound() {
// given
long id = 10;
given(studentRepository.existsById(id))
.willReturn(false);
// when
// then
assertThatThrownBy(() -> underTest.deleteStudent(id))
.isInstanceOf(StudentNotFoundException.class)
.hasMessageContaining("Student with id " + id + " does not exists");
verify(studentRepository, never()).deleteById(any());
}
}