-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskServiceTest.java
More file actions
123 lines (94 loc) · 3.72 KB
/
TaskServiceTest.java
File metadata and controls
123 lines (94 loc) · 3.72 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
114
115
116
117
118
119
120
121
122
123
package com.lucc.taskmanager.service;
import com.lucc.taskmanager.model.Priority;
import com.lucc.taskmanager.model.Status;
import com.lucc.taskmanager.model.Task;
import com.lucc.taskmanager.model.User;
import com.lucc.taskmanager.repository.TaskRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class TaskServiceTest {
@Mock
private TaskRepository taskRepository;
@InjectMocks
private TaskService taskService;
private User user;
private Task task;
@BeforeEach
void setUp() {
user = new User();
user.setUsername("testuser");
task = new Task();
task.setId(1);
task.setTitle("Test Task");
task.setDescription("Test Description");
task.setUser(user);
task.setStatus(Status.TODO);
task.setPriority(Priority.MEDIUM);
}
@Test
void getTasksByUser_ShouldReturnTasks() {
when(taskRepository.findByUser(user)).thenReturn(Arrays.asList(task));
List<Task> tasks = taskService.getTasksByUser(user);
assertEquals(1, tasks.size());
assertEquals(task.getTitle(), tasks.get(0).getTitle());
verify(taskRepository, times(1)).findByUser(user);
}
@Test
void addTask_ShouldSaveTask() {
when(taskRepository.save(any(Task.class))).thenReturn(task);
Task savedTask = taskService.addTask(new Task(), user);
assertNotNull(savedTask);
assertEquals(user, savedTask.getUser());
verify(taskRepository, times(1)).save(any(Task.class));
}
@Test
void getTaskById_ShouldReturnTask_WhenTaskExistsAndUserMatches() {
when(taskRepository.findById(1)).thenReturn(Optional.of(task));
Task foundTask = taskService.getTaskById(1, user);
assertNotNull(foundTask);
assertEquals(1, foundTask.getId());
}
@Test
void getTaskById_ShouldThrowException_WhenTaskDoesNotExist() {
when(taskRepository.findById(1)).thenReturn(Optional.empty());
assertThrows(IllegalArgumentException.class, () -> taskService.getTaskById(1, user));
}
@Test
void getTaskById_ShouldThrowException_WhenUserDoesNotMatch() {
User otherUser = new User();
otherUser.setUsername("otheruser");
when(taskRepository.findById(1)).thenReturn(Optional.of(task));
assertThrows(IllegalArgumentException.class, () -> taskService.getTaskById(1, otherUser));
}
@Test
void updateTask_ShouldUpdateAndSaveTask() {
Task updatedInfo = new Task();
updatedInfo.setTitle("Updated Title");
updatedInfo.setDescription("Updated Description");
updatedInfo.setStatus(Status.DONE);
updatedInfo.setPriority(Priority.HIGH);
when(taskRepository.findById(1)).thenReturn(Optional.of(task));
when(taskRepository.save(any(Task.class))).thenReturn(task);
Task result = taskService.updateTask(1, updatedInfo, user);
assertEquals("Updated Title", result.getTitle());
assertEquals(Status.DONE, result.getStatus());
verify(taskRepository, times(1)).save(task);
}
@Test
void deleteTask_ShouldDeleteTask_WhenTaskExistsAndUserMatches() {
when(taskRepository.findById(1)).thenReturn(Optional.of(task));
taskService.deleteTask(1, user);
verify(taskRepository, times(1)).delete(task);
}
}