Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestjx committed Oct 22, 2024
1 parent e5817ad commit ddbca4a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jacocoTestReport {

afterEvaluate {
excludedClassFilesForReport(classDirectories)

}
}

Expand Down Expand Up @@ -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/*"
}
}

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quemistry.statistics_ms.exception;
package com.quemistry.statistics_ms.model;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit ddbca4a

Please sign in to comment.