/
podkmax
/
spring-test
Обзор
Документация
Войти
/
podkmax
/
spring-test
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/example/springtests/controller/UserControllerIntegrationTest.java
173 строки
7 KB
Maksim.Podkorytov
sb test example
04 май 2026, 15:40
04 май 2026, 15:40
8288ba1
Код
Авторство
О чём код?
package com.example.springtests.controller; import com.example.springtests.AbstractApplicationTest; import com.example.springtests.dto.CreateUserRequest; import com.example.springtests.dto.UpdateUserRequest; import com.example.springtests.dto.UserResponse; import com.example.springtests.repository.UserRepository; import java.nio.charset.StandardCharsets; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; 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.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class UserControllerIntegrationTest extends AbstractApplicationTest { @Autowired private UserRepository userRepository; @BeforeEach void cleanDatabase() { userRepository.deleteAll(); } @Test void shouldCreateUser() throws Exception { CreateUserRequest request = new CreateUserRequest(" John Doe!! ", "john@example.com"); String responseBody = mockMvc.perform(post("/api/users") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isCreated()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); UserResponse createdUser = objectMapper.readValue(responseBody, UserResponse.class); assertThat(createdUser.id()).isNotNull(); assertThat(createdUser.name()).isEqualTo("john doe"); assertThat(createdUser.email()).isEqualTo("john@example.com"); } @Test void shouldGetUserById() throws Exception { Long userId = createUser("John Doe", "john@example.com").id(); String responseBody = mockMvc.perform(get("/api/users/{id}", userId)) .andExpect(status().isOk()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); UserResponse user = objectMapper.readValue(responseBody, UserResponse.class); assertThat(user.id()).isEqualTo(userId); assertThat(user.email()).isEqualTo("john@example.com"); } @Test void shouldGetAllUsers() throws Exception { createUser("John Doe", "john@example.com"); createUser("Mary Jane", "mary@example.com"); String responseBody = mockMvc.perform(get("/api/users")) .andExpect(status().isOk()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); UserResponse[] users = objectMapper.readValue(responseBody, UserResponse[].class); assertThat(List.of(users)).hasSize(2); } @Test void shouldUpdateUser() throws Exception { Long userId = createUser("John Doe", "john@example.com").id(); UpdateUserRequest request = new UpdateUserRequest(" MARY*** Ann ", "mary@example.com"); String responseBody = mockMvc.perform(put("/api/users/{id}", userId) .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isOk()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); UserResponse updatedUser = objectMapper.readValue(responseBody, UserResponse.class); assertThat(updatedUser.id()).isEqualTo(userId); assertThat(updatedUser.name()).isEqualTo("mary ann"); assertThat(updatedUser.email()).isEqualTo("mary@example.com"); } @Test void shouldDeleteUser() throws Exception { Long userId = createUser("John Doe", "john@example.com").id(); mockMvc.perform(delete("/api/users/{id}", userId)) .andExpect(status().isNoContent()); mockMvc.perform(get("/api/users/{id}", userId)) .andExpect(status().isNotFound()); } @Test void shouldReturnConflictWhenEmailAlreadyExists() throws Exception { createUser("John Doe", "john@example.com"); CreateUserRequest duplicateRequest = new CreateUserRequest("Mary Jane", "john@example.com"); String responseBody = mockMvc.perform(post("/api/users") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(duplicateRequest))) .andExpect(status().isConflict()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); ErrorResponse response = objectMapper.readValue(responseBody, ErrorResponse.class); assertThat(response.message()).isEqualTo("User with this email already exists"); } @Test void shouldReturnBadRequestWhenEmailIsInvalid() throws Exception { CreateUserRequest invalidRequest = new CreateUserRequest("John Doe", "not-an-email"); String responseBody = mockMvc.perform(post("/api/users") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(invalidRequest))) .andExpect(status().isBadRequest()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); ErrorResponse response = objectMapper.readValue(responseBody, ErrorResponse.class); assertThat(response.message()).contains("email"); } @Test void shouldReturnBadRequestWhenNameIsBlank() throws Exception { CreateUserRequest invalidRequest = new CreateUserRequest(" ", "john@example.com"); String responseBody = mockMvc.perform(post("/api/users") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(invalidRequest))) .andExpect(status().isBadRequest()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); ErrorResponse response = objectMapper.readValue(responseBody, ErrorResponse.class); assertThat(response.message()).contains("name"); } private UserResponse createUser(String name, String email) throws Exception { CreateUserRequest request = new CreateUserRequest(name, email); String responseBody = mockMvc.perform(post("/api/users") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isCreated()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); return objectMapper.readValue(responseBody, UserResponse.class); } private record ErrorResponse(String message) { } }