From b32dc777345f632c74f0ec4574f3673cfb8af7db Mon Sep 17 00:00:00 2001 From: rayankonecny Date: Sat, 13 Dec 2025 14:09:20 +0000 Subject: [PATCH] add integration test to find by id user controller with not found --- .../impl/UserControllerImpTest.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/user-service-api/src/test/java/br/com/rayankonecny/userserviceapi/controller/impl/UserControllerImpTest.java b/user-service-api/src/test/java/br/com/rayankonecny/userserviceapi/controller/impl/UserControllerImpTest.java index 466c3fc..06c64c6 100644 --- a/user-service-api/src/test/java/br/com/rayankonecny/userserviceapi/controller/impl/UserControllerImpTest.java +++ b/user-service-api/src/test/java/br/com/rayankonecny/userserviceapi/controller/impl/UserControllerImpTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; @@ -32,15 +33,23 @@ public class UserControllerImpTest { final var entity = generateMock(User.class); final var userId = userRepository.save(entity).getId(); - mockMvc.perform(get("/api/users/{id}", userId)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.id").value(userId)) - .andExpect(jsonPath("$.name").value(entity.getName())) - .andExpect(jsonPath("$.email").value(entity.getEmail())) - .andExpect(jsonPath("$.password").value(entity.getPassword())) - .andExpect(jsonPath("$.profiles").isArray()); + mockMvc.perform(get("/api/users/{id}", userId)).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(userId)) + .andExpect(jsonPath("$.name").value(entity.getName())).andExpect(jsonPath("$.email").value(entity.getEmail())) + .andExpect(jsonPath("$.password").value(entity.getPassword())).andExpect(jsonPath("$.profiles").isArray()); userRepository.deleteById(userId); } + @Test + void testFindByIdWithNotFoundException() throws Exception { + mockMvc.perform(get("/api/users/{id}", "123")).andExpect(status().isNotFound()) + .andExpect(jsonPath("$.message").value("Object not found! Id: 123, Type: UserResponse")) + .andExpect(jsonPath("$.error").value(HttpStatus.NOT_FOUND.getReasonPhrase())) + .andExpect(jsonPath("$.path").value("/api/users/123")) + .andExpect(jsonPath("$.status").value(HttpStatus.NOT_FOUND.value())) + .andExpect(jsonPath("$.timestamp").isNotEmpty()); + + // testando + } + }