Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
farrell-m committed Jun 6, 2024
1 parent c98663d commit 74e80ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 64 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package uk.gov.laa.ccms.caab.assessment.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
Expand All @@ -8,17 +12,20 @@
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.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
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.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import uk.gov.laa.ccms.caab.assessment.exception.ApplicationException;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetail;
Expand All @@ -29,16 +36,24 @@
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
class AssessmentControllerTest {

@MockBean
@Mock
private AssessmentService assessmentService;

@Autowired
@InjectMocks
private AssessmentController applicationController;

private MockMvc mockMvc;

@BeforeEach
public void setup() {
mockMvc = standaloneSetup(applicationController)
.build();
}

@Test
public void createAssessment_createsAssessmentSuccessfully() throws Exception {
Long assessmentId = 1L;
Expand Down Expand Up @@ -71,7 +86,7 @@ public void getAssessment() throws Exception {
}

@Test
public void getAssessment_throwsNotFound() throws Exception {
public void getAssessment_throwsNotFound() {
Long assessmentId = 1L;
String errorMessage = String.format("Assessment with id %s not found", assessmentId);

Expand All @@ -81,10 +96,16 @@ public void getAssessment_throwsNotFound() throws Exception {
errorMessage,
HttpStatus.NOT_FOUND));

this.mockMvc.perform(get("/assessments/{assessment-id}", assessmentId))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.error_message").value(errorMessage))
.andExpect(jsonPath("$.http_status").value(HttpStatus.NOT_FOUND.value()));
ServletException ex = assertThrows(ServletException.class, () ->
this.mockMvc.perform(get("/assessments/{assessment-id}", assessmentId)),
"Expected ServletException to be thrown, but wasn't.");

assertTrue(ex.getMessage().contains(errorMessage));
assertInstanceOf(ApplicationException.class, ex.getRootCause());

ApplicationException appEx = (ApplicationException) ex.getRootCause();
assertEquals(HttpStatus.NOT_FOUND, appEx.getHttpStatus());
assertEquals(errorMessage, appEx.getErrorMessage());

verify(assessmentService).getAssessment(assessmentId);
}
Expand Down

0 comments on commit 74e80ec

Please sign in to comment.