Skip to content

Commit

Permalink
add controller test unit testingfile
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsage1 committed Nov 8, 2023
1 parent 599bf81 commit c2eb562
Showing 1 changed file with 59 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
// package org.sagebionetworks.openchallenges.image.service.exception;

// import static org.assertj.core.api.Assertions.assertThat;
// import static org.mockito.Mockito.when;

// import org.junit.jupiter.api.Test;
// import org.mockito.InjectMocks;
// import org.mockito.Mock;
// import org.mockito.MockitoAnnotations;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
// import org.springframework.http.HttpStatus;
// import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
// import org.springframework.test.web.servlet.MockMvc;
// import org.springframework.validation.BindException;
// import org.springframework.validation.BindingResult;
// import org.springframework.validation.FieldError;

// @SpringJUnitConfig
// @WebMvcTest(ControllerAdvisor.class)
// public class ControllerAdvisorTest {

// @Autowired private MockMvc mockMvc;

// @Mock private BindException bindException;

// @InjectMocks private ControllerAdvisor controllerAdvisor;

// @Test
// public void bindException_ShouldReturnResultAndException_WhenMockErrorIsPassed() {
// MockitoAnnotations.openMocks(this);

// // String bindException = "There is an error";
// String header = "Exception header";
// String status = HttpStatus.BAD_REQUEST;
// String webRequest = "Exception web request";

// // Create a mock FieldError
// FieldError fieldError =
// new FieldError(
// "objectName",
// "fieldName",
// "rejectedValue",
// false,
// new String[] {"errorCode"},
// null,
// "defaultMessage");

// // Create a mock BindingResult
// BindingResult bindingResult = new BindException(new Object(), "objectName");
// bindingResult.addError(fieldError);
// BindingResult handleBindException =
// new HandleBindException(bindException, header, status, webRequest);

// // // Set up the mock BindException to return the mock BindingResult
// when(bindException.getBindingResult()).thenReturn(bindingResult);
// when(bindException.handleBindException()).thenReturn(handleBindException);

// assertThat(bindException.getBindingResult()).isEqualTo(bindingResult);
// assertThat(bindException.handleBindException()).isEqualTo(handleBindException.get(0));
// }
// }
package org.sagebionetworks.openchallenges.image.service.exception;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import org.sagebionetworks.openchallenges.image.service.exception.GlobalExceptionHandler;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.sagebionetworks.openchallenges.image.service.model.dto.BasicErrorDto;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.context.request.WebRequest;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ControllerAdvisorTest {

@Test
public void testHandleBindException() {
// Create a sample BindException
BindException bindException = mock(BindException.class);
BindingResult bindingResult = mock(BindingResult.class);
FieldError fieldError = new FieldError("objectName", "fieldName", "rejectedValue", false, null, null, "error message");
List<FieldError> fieldErrors = new ArrayList<>();
fieldErrors.add(fieldError);

// Mock the behavior of the BindException and BindingResult
when(bindException.getBindingResult()).thenReturn(bindingResult);
when(bindingResult.getFieldErrors()).thenReturn(fieldErrors);

// Create the ControllerAdvisor instance
ControllerAdvisor controllerAdvisor = new ControllerAdvisor();

// Call handleBindException
ResponseEntity<Object> responseEntity = controllerAdvisor.handleBindException(bindException, new HttpHeaders(), HttpStatus.BAD_REQUEST, mock(WebRequest.class));

// Verify the response
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
BasicErrorDto errorDto = (BasicErrorDto) responseEntity.getBody();
}
}

0 comments on commit c2eb562

Please sign in to comment.