-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathStudentServiceTest.java
More file actions
108 lines (77 loc) · 3.12 KB
/
StudentServiceTest.java
File metadata and controls
108 lines (77 loc) · 3.12 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
package com.example.demo.student;
import com.example.demo.student.exception.BadRequestException;
import com.example.demo.student.exception.StudentNotFoundException;
import net.bytebuddy.matcher.ElementMatchers;
import org.assertj.core.api.Assertions;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.List;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class StudentServiceTest {
@Mock
private StudentRepository studentRepository;
private StudentService underTest;
private Student student;
@BeforeEach
void setUp() {
underTest = new StudentService(studentRepository);
student = new Student("amir", "fathiamir37@gmail.com", Gender.MALE);
}
@Test
void canGetAllStudents() {
// Arrange
List<Student> studentList = Arrays.asList(
new Student("amir", "fathiamir37@gmail.com", Gender.MALE),
new Student("sara", "saraamir37@gmail.com", Gender.FEMALE));
when(studentRepository.findAll()).thenReturn(studentList);
// Act
List<Student> allStudents = underTest.getAllStudents();
// Assert
Assertions.assertThat(studentList.size()).isEqualTo(allStudents.size());
// Verify
Mockito.verify(studentRepository).findAll();
}
@Test
void shouldAddStudent() {
underTest.addStudent(student);
ArgumentCaptor<Student> studentArgumentCaptor = ArgumentCaptor.forClass(Student.class);
Mockito.verify(studentRepository).save(studentArgumentCaptor.capture());
Student value = studentArgumentCaptor.getValue();
Assertions.assertThat(value).isEqualTo(student);
}
@Test
void shouldGetExceptionWhenAddingStudent() {
when(studentRepository.selectExistsEmail(ArgumentMatchers.any())).thenReturn(true);
Assertions.assertThatThrownBy(() -> underTest.addStudent(student))
.isInstanceOfAny(BadRequestException.class);
Mockito.verify(studentRepository, Mockito.never()).save(ArgumentMatchers.any());
}
@Test
void shouldBeAbleToDeleteStudent() {
Long studentId = 1L;
when(studentRepository.existsById(studentId)).thenReturn(true);
underTest.deleteStudent(studentId);
verify(studentRepository, times(1)).deleteById(studentId);
}
@Test
void shouldGetExceptionWhenDeleteStudent() {
when(studentRepository
.existsById(ArgumentMatchers.any()))
.thenReturn(false);
Assertions
.assertThatThrownBy(() -> underTest.deleteStudent(121L))
.isInstanceOf(StudentNotFoundException.class);
Mockito
.verify(studentRepository, Mockito.never()).deleteById(ArgumentMatchers.any());
}
}