/
dmuruz
/
Course_Service
Обзор
Документация
Войти
/
dmuruz
/
Course_Service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
src/test/java/org/example/manager/LessonManagerTest.java
90 строк
3 KB
Danil Muruz
MVP
24 апр 2025, 09:39
24 апр 2025, 09:39
16b1c45
Код
Авторство
О чём код?
package org.example.manager; import org.example.dao.CourseRepository; import org.example.dao.LessonRepository; import org.example.dto.LessonDTO; import org.example.model.Course; import org.example.model.Lesson; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; 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.*; class LessonManagerTest { @Mock private LessonRepository lessonRepository; @Mock private CourseRepository courseRepository; @InjectMocks private LessonManager lessonManager; private Lesson lesson; private Course course; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); course = new Course(); course.setId(1L); lesson = new Lesson(); lesson.setId(2L); lesson.setCourse(course); } @Test void createLesson_ShouldSave() { LessonDTO dto = new LessonDTO(); dto.setName("L"); dto.setDescription("D"); dto.setJsonSchema("{}"); dto.setDurationSteps(3); dto.setOrderNum(1); dto.setCourseId(1L); when(courseRepository.findById(1L)).thenReturn(Optional.of(course)); when(lessonRepository.save(any(Lesson.class))).thenAnswer(i -> i.getArgument(0)); Lesson saved = lessonManager.createLesson(dto); assertEquals("L", saved.getName()); verify(lessonRepository).save(any(Lesson.class)); } @Test void getAllLessons_ShouldReturnAll() { when(lessonRepository.findAll()).thenReturn(List.of(lesson)); List<Lesson> list = lessonManager.getAllLessons(); assertEquals(1, list.size()); } @Test void getLessonById_Found() { when(lessonRepository.findById(2L)).thenReturn(Optional.of(lesson)); Optional<Lesson> opt = lessonManager.getLessonById(2L); assertTrue(opt.isPresent()); } @Test void updateLesson_ShouldModify() { LessonDTO dto = new LessonDTO(); dto.setName("Updated"); when(lessonRepository.findById(2L)).thenReturn(Optional.of(lesson)); when(lessonRepository.save(lesson)).thenReturn(lesson); Lesson updated = lessonManager.updateLesson(2L, dto); assertEquals("Updated", updated.getName()); } @Test void deleteLesson_ShouldInvoke() { doNothing().when(lessonRepository).deleteById(2L); lessonManager.deleteLesson(2L); verify(lessonRepository).deleteById(2L); } @Test void getFirstLesson_ShouldReturn() { when(courseRepository.findById(1L)).thenReturn(Optional.of(course)); when(lessonRepository.findFirstByCourseOrderByOrderNumAsc(course)).thenReturn(lesson); Lesson first = lessonManager.getFirstLesson(1L); assertEquals(lesson, first); } }