/
dmuruz
/
Course_Service
Обзор
Документация
Войти
/
dmuruz
/
Course_Service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
src/test/java/org/example/controller/LessonControllerTest.java
88 строк
3 KB
Danil Muruz
MVP
24 апр 2025, 09:39
24 апр 2025, 09:39
16b1c45
Код
Авторство
О чём код?
package org.example.controller; import org.example.config.DisableSecurityConfig; import org.example.manager.LessonManager; import org.example.manager.UserProgressManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.example.model.Lesson; import org.junit.jupiter.api.Test; import java.util.Optional; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(LessonController.class) @Import(DisableSecurityConfig.class) class LessonControllerTest { @Autowired private MockMvc mvc; @MockBean private LessonManager lessonManager; @MockBean private UserProgressManager userProgressManager; @Test void createLesson() throws Exception { Lesson l = new Lesson(); l.setId(10L); when(lessonManager.createLesson(any())).thenReturn(l); mvc.perform(post("/lessons") .contentType(MediaType.APPLICATION_JSON) .content("{\"name\":\"L\",\"courseId\":1}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(10)); } @Test void getLessonById() throws Exception { Lesson l = new Lesson(); l.setId(11L); when(lessonManager.getLessonById(11L)).thenReturn(Optional.of(l)); mvc.perform(get("/lessons/11")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(11)); } @Test void getLessonByIdNotFound() throws Exception { when(lessonManager.getLessonById(99L)).thenReturn(Optional.empty()); mvc.perform(get("/lessons/99")) .andExpect(status().isNotFound()); } @Test void updateLesson() throws Exception { Lesson updated = new Lesson(); updated.setId(12L); when(lessonManager.updateLesson(eq(12L), any())).thenReturn(updated); mvc.perform(put("/lessons/12") .contentType(MediaType.APPLICATION_JSON) .content("{\"name\":\"Updated\"}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(12)); } @Test void deleteLesson() throws Exception { doNothing().when(lessonManager).deleteLesson(13L); mvc.perform(delete("/lessons/13")) .andExpect(status().isOk()); } @Test void getFirstLesson() throws Exception { Lesson first = new Lesson(); first.setId(14L); when(lessonManager.getFirstLesson(2L)).thenReturn(first); mvc.perform(get("/lessons/first/2")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(14)); } }