Initial commit - Task Manager REST API
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package dev.deklab.taskmanager.service;
|
||||
|
||||
import dev.deklab.taskmanager.model.Task;
|
||||
import dev.deklab.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.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class TaskServiceTest {
|
||||
|
||||
@Mock
|
||||
private TaskRepository taskRepository;
|
||||
|
||||
@InjectMocks
|
||||
private TaskService taskService;
|
||||
|
||||
private Task task;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
task = new Task("Finir le CV", "Envoyer à des employeurs");
|
||||
task.setCompleted(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllTasks_shouldReturnList() {
|
||||
when(taskRepository.findAll()).thenReturn(List.of(task));
|
||||
|
||||
List<Task> result = taskService.getAlltask();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Finir le CV", result.get(0).getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTask_shouldSaveAndReturn() {
|
||||
when(taskRepository.save(task)).thenReturn(task);
|
||||
|
||||
Task result = taskService.createTask(task);
|
||||
|
||||
assertEquals("Finir le CV", result.getTitle());
|
||||
verify(taskRepository, times(1)).save(task);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTaskById_shouldReturnTask() {
|
||||
when(taskRepository.findById(1L)).thenReturn(Optional.of(task));
|
||||
|
||||
Task result = taskService.getTaskById(1L);
|
||||
|
||||
assertEquals("Finir le CV", result.getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTaskById_shouldThrowWhenNotFound() {
|
||||
when(taskRepository.findById(99L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> taskService.getTaskById(99L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTask_shouldCallRepository() {
|
||||
taskService.deleteTask(1L);
|
||||
|
||||
verify(taskRepository, times(1)).deleteById(1L);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user