diff --git a/build.gradle b/build.gradle index d4dacf4..a2157f2 100644 --- a/build.gradle +++ b/build.gradle @@ -75,7 +75,6 @@ jacocoTestReport { afterEvaluate { excludedClassFilesForReport(classDirectories) - } } @@ -113,7 +112,7 @@ sonar { property "sonar.projectKey", "mtech-31-quemistry_statistics_ms" property "sonar.organization", "mtech-31-quemistry" property "sonar.host.url", "https://sonarcloud.io" - property "sonar.coverage.exclusions", "**/entity/*,**/model/**/*,**/model/*,**/entity/**/*,**/Application,**/constant/**/*,**/constant/*,com/quemistry/statistics_ms/exception/ErrorResponse" + property "sonar.coverage.exclusions", "**/entity/*,**/model/**/*,**/model/*,**/entity/**/*,**/Application,**/constant/**/*,**/constant/*" } } diff --git a/src/main/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandler.java b/src/main/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandler.java index fe2e4aa..3574635 100644 --- a/src/main/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandler.java @@ -1,5 +1,6 @@ package com.quemistry.statistics_ms.exception; +import com.quemistry.statistics_ms.model.ErrorResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/src/main/java/com/quemistry/statistics_ms/exception/ErrorResponse.java b/src/main/java/com/quemistry/statistics_ms/model/ErrorResponse.java similarity index 87% rename from src/main/java/com/quemistry/statistics_ms/exception/ErrorResponse.java rename to src/main/java/com/quemistry/statistics_ms/model/ErrorResponse.java index 5d5de6c..79ecafb 100644 --- a/src/main/java/com/quemistry/statistics_ms/exception/ErrorResponse.java +++ b/src/main/java/com/quemistry/statistics_ms/model/ErrorResponse.java @@ -1,4 +1,4 @@ -package com.quemistry.statistics_ms.exception; +package com.quemistry.statistics_ms.model; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/src/test/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandlerTest.java b/src/test/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandlerTest.java new file mode 100644 index 0000000..373d4e1 --- /dev/null +++ b/src/test/java/com/quemistry/statistics_ms/exception/GlobalExceptionHandlerTest.java @@ -0,0 +1,50 @@ +package com.quemistry.statistics_ms.exception; + +import com.quemistry.statistics_ms.controller.StatisticsController; +import org.junit.jupiter.api.BeforeEach; +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.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.junit.jupiter.api.Assertions.*; + +@WebMvcTest(controllers = GlobalExceptionHandler.class) +class GlobalExceptionHandlerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private StatisticsController controller; + + @Test + void whenRuntimeExceptionThrown_thenInternalServerErrorResponse() throws Exception { + // Given + doThrow(new RuntimeException("Test RuntimeException")) + .when(controller) + .getMcqStatistics(any(), any()); // This will trigger the exception + + // When & Then + MvcResult result = mockMvc.perform(get("/v1/stats/mcq") + .contentType(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isInternalServerError()) + .andExpect(jsonPath("$.statusCode").value(HttpStatus.INTERNAL_SERVER_ERROR.value())) + .andExpect(jsonPath("$.message").value("Internal Server Error")) + .andReturn(); + + // Assert + String responseBody = result.getResponse().getContentAsString(); + assertTrue(responseBody.contains("Internal Server Error")); + } +}