/
dmuruz
/
Course_Service
Обзор
Документация
Войти
/
dmuruz
/
Course_Service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
src/test/java/org/example/controller/CourseControllerTest.java
99 строк
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.CourseManager; import org.example.model.Course; import org.example.model.Lesson; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.context.annotation.Import; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import java.util.List; 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.*; @WebMvcTest(CourseController.class) @Import(DisableSecurityConfig.class) class CourseControllerTest { @Autowired private MockMvc mvc; @MockBean private CourseManager courseManager; @Test void createCourse() throws Exception { Course c = new Course(); c.setId(1L); c.setName("C"); when(courseManager.createCourse(any())).thenReturn(c); mvc.perform(post("/courses") .contentType(MediaType.APPLICATION_JSON) .content("{\"name\":\"C\",\"description\":\"D\",\"orderNum\":1}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(1)); } @Test void getAllCourses() throws Exception { when(courseManager.getAllCourses()).thenReturn(List.of(new Course())); mvc.perform(get("/courses")) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()").value(1)); } @Test void getCourseById_Found() throws Exception { Course c = new Course(); c.setId(2L); when(courseManager.getCourseById(2L)).thenReturn(Optional.of(c)); mvc.perform(get("/courses/2")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(2)); } @Test void getCourseById_NotFound() throws Exception { when(courseManager.getCourseById(99L)).thenReturn(Optional.empty()); mvc.perform(get("/courses/99")) .andExpect(status().isNotFound()); } @Test void updateCourse() throws Exception { Course updated = new Course(); updated.setId(3L); when(courseManager.updateCourse(eq(3L), any())).thenReturn(updated); mvc.perform(put("/courses/3") .contentType(MediaType.APPLICATION_JSON) .content("{\"name\":\"X\"}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(3)); } @Test void deleteCourse() throws Exception { doNothing().when(courseManager).deleteCourse(4L); mvc.perform(delete("/courses/4")) .andExpect(status().isOk()); } @Test void getLessonsByCourse() throws Exception { Lesson lesson = new Lesson(); lesson.setId(5L); when(courseManager.getLessonsByCourseId(1L)).thenReturn(List.of(lesson)); mvc.perform(get("/courses/1/lessons")) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id").value(5)); } }