/
marie.sidorkina
/
eye_tracking_java
Обзор
Документация
Войти
/
marie.sidorkina
/
eye_tracking_java
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/example/mydemo1/SessionControllerTest.java
153 строки
6 KB
Marie
add test for all user sessions
14 янв 2026, 21:42
14 янв 2026, 21:42
04e7264
Код
Авторство
О чём код?
package com.example.mydemo1; import com.example.mydemo1.dto.SessionCreateRequest; import com.example.mydemo1.dto.SessionDto; import com.example.mydemo1.dto.SessionEndRequest; import com.example.mydemo1.dto.SessionDetailsDto; import com.example.mydemo1.service.SessionService; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.http.MediaType; import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; import java.time.Instant; import java.util.List; import static org.hamcrest.Matchers.hasSize; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest(controllers = com.example.mydemo1.controller.SessionController.class) @AutoConfigureMockMvc(addFilters = false) class SessionControllerTest { @Autowired private MockMvc mockMvc; @MockitoBean private SessionService sessionService; @Autowired private ObjectMapper objectMapper; @Test @DisplayName("POST /api/sessions -> 201 and returns SessionDto") void createSession_returnsCreatedDto() throws Exception { SessionCreateRequest req = new SessionCreateRequest(); req.setUserId(100L); req.setName("my session"); req.setStartedAt(Instant.now()); SessionDto dto = new SessionDto(); dto.setId(55L); dto.setName("my session"); dto.setStartedAt(req.getStartedAt()); Mockito.when(sessionService.createSession(any(SessionCreateRequest.class))).thenReturn(dto); mockMvc.perform(post("/api/sessions") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(req))) .andExpect(status().isCreated()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.id").value(55)) .andExpect(jsonPath("$.name").value("my session")); } @Test @DisplayName("POST /api/sessions/{id}/end -> 200 and returns SessionDto with endedAt") void endSession_returnsUpdatedSession() throws Exception { long sessionId = 55L; SessionEndRequest req = new SessionEndRequest(); req.setEndedAt(Instant.now()); SessionDto returned = new SessionDto(); returned.setId(sessionId); returned.setName("my session"); returned.setEndedAt(req.getEndedAt()); Mockito.when(sessionService.endSession(eq(sessionId), any(SessionEndRequest.class))) .thenReturn(returned); mockMvc.perform(post("/api/sessions/{id}/end", sessionId) .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(req))) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(sessionId)) .andExpect(jsonPath("$.endedAt").exists()); } @Test @DisplayName("GET /api/sessions/{id} -> 200 and returns SessionDto") void getSession_returnsSessionDto() throws Exception { long sessionId = 77L; SessionDto dto = new SessionDto(); dto.setId(sessionId); dto.setName("session77"); Mockito.when(sessionService.getSession(sessionId)).thenReturn(dto); mockMvc.perform(get("/api/sessions/{id}", sessionId)) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(sessionId)) .andExpect(jsonPath("$.name").value("session77")); } @Test @DisplayName("GET /api/sessions/{id}/details -> 200 and returns SessionDetailsDto") void getSessionDetails_returnsDetails() throws Exception { long sessionId = 88L; SessionDetailsDto details = new SessionDetailsDto(); details.setId(sessionId); details.setName("session88"); details.setChunks(List.of()); details.setHints(List.of()); Mockito.when(sessionService.getSessionDetails(sessionId)).thenReturn(details); mockMvc.perform(get("/api/sessions/{id}/details", sessionId)) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(sessionId)) .andExpect(jsonPath("$.chunks").isArray()) .andExpect(jsonPath("$.hints").isArray()); } @Test @DisplayName("GET /api/sessions?userId={userId} -> 200 and returns list of SessionDto") void getSessionsByUser_returnsList() throws Exception { long userId = 100L; SessionDto s1 = new SessionDto(); s1.setId(1L); s1.setName("Morning read"); s1.setStartedAt(Instant.parse("2025-01-10T09:00:00Z")); SessionDto s2 = new SessionDto(); s2.setId(2L); s2.setName("Evening read"); s2.setStartedAt(Instant.parse("2025-01-11T20:30:00Z")); List<SessionDto> mocked = List.of(s2, s1); Mockito.when(sessionService.getSessionsByUserId(eq(userId))).thenReturn(mocked); mockMvc.perform(get("/api/sessions?userId={userId}", userId) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[0].id").value(2)) .andExpect(jsonPath("$[0].name").value("Evening read")) .andExpect(jsonPath("$[1].id").value(1)) .andExpect(jsonPath("$[1].name").value("Morning read")); } }